Quiz Questions
Each question has four choices. More than one options can be correct. When you have finished the quiz, click the button at the end of the questions to view the result, and the solutions and answers.
Correct Answers: | |
Wrong Answers: | |
Unattempted: |
Question 1
Consider the code below.
void MyFunc(int& i) { i = i*2; return; } int main () { int x = 100; MyFunc (x); MyFunc (x); cout << x << endl; }Which of the following is correct ?
A
MyFunc accepts i by reference, and cout will display 400
B
MyFunc accepts i by value, and cout will display 400
C
MyFunc accepts i by reference, and cout will display 100
D
MyFunc accepts i by value, and cout will display 100
Soln.
Ans: A
Ans: A
MyFunc accepts i by reference, so when x is passed as an argument it goes as its alias and any changes that occur inside MyFunc actually occur on x. MyFunc is called twice above, so cout will display 400.
Question 2
Which statement has error in the code below
int c = 9; int main() { /*Statement 1*/ int b; /*Statement 2*/ b = 10; /*Statement 3*/ a = c++; /*Statement 4*/ c = a; return 0; }
A
Statement 1. and Statement 3.
B
Statement 1. and Statement 2.
C
Statement 4. and Statement 3.
D
Statement 2. and Statement 3.
Soln.
Ans: C
Ans: C
Statement 3 and 4 have error because they have an undeclared variable that is not in scope
Question 3
Which of the following is true -
A
A function that doesn't return void must accept arguments.
B
A function that returns void must accept arguments.
C
A function that returns void may or may not accept any arguments.
D
A function that returns void cannot accept arguments.
Soln.
Ans: C
Ans: C
A function may or may not accept arguments and can return void or any valid data type.
Question 4
What is the output of the following program ?
void main() { char *p; p="Hello"; printf("%c\n",*&*p); }
A
Compiler Error
B
Hello
C
H
D
garbage
Soln.
Ans: C
Ans: C
* is a dereference operator & is a reference operator. They can be applied anynumber of times provided it is meaningful. Here p points to the first character in thestring "Hello". *p dereferences it and so its value is H. Again & references it to anaddress and * dereferences it to the value H.
Question 5
What will be output if you will compile and execute the following code?
void main() { int i=10; static int x=i; if(x==i) { printf("Equal"); } else if(x>i) { printf("Greater than"); } else { printf("Less than"); } }
A
Equal
B
Greater than
C
Less than
D
Compiler Error
Soln.
Ans: A
Ans: A
It is simple, x is a static variable that gets the value i which is 10. The first 'if' becomes true. [Compile with VC++ compiler and check]
Question 6
Which of the codes below will enable us to count the number of occurrences of 'a' in the character array cX ?
/*Code 1*/ int main() { char cX [] = "I am an Indian"; int j = 0; for (int i = 0; i < sizeof (cX)/sizeof (char); i++) { if (cX [i] == 'a') { j++; } } return 0; } /*Code 2*/ int main() { char cX [] = "I am an Indian"; for (int i = 0, j = 0; i < sizeof (cX)/sizeof (char); i++) { if (cX [i] == 'a') { j++; } } return 0; } /*Code 3*/ int main() { char cX [] = "I am an Indian"; for (int i = 0; i < sizeof (cX)/sizeof (char); i++) { int j = 0; if (cX [i] == 'a') { j++; } } return 0; }
A
Code 1
B
Code 2
C
Code 3
D
ALL
Soln.
Ans: A
Ans: A
Only code 1. because j is available outside the loop only in code 1.
Question 7
What is the output ?
void main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year", y); else printf("%d is not a leap year", y); }
A
2000 is a leap year
B
2000 is not a leap year
C
Compiler Error
D
Linker Error
Soln.
Ans: A
Ans: A
This is an ordinary program to check if leap year or not.
Question 8
What is the output of the following program ?
void main() { int a[] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf("%d" ,*a); a++; } p = a; for(j=0; j<5; j++) { printf("%d " ,*p); p++; } }
A
Compiler Error in both C++ and C compilers
B
Garbage value
C
linker error
D
Compiler error only in C++
Soln.
Ans: A
Ans: A
The statement
a++is invalid because 'a' being an array identifier is an l-value.
Question 9
What is the output ?
int main () { int x = 1, y = 2; if (--x && --y) { printf ("x = %d, y = %d", x, y); }else { printf ("%d, %d", x, y); } return 0; }
A
0 1
B
x=0 y=1
C
x=1 y=2
D
None
Soln.
Ans: D
Ans: D
The output is 0, 2
Question 10
What is the output ?
void main() { // assume 32-bit addresses char *str1="abcd"; char str2[]="abcd"; printf( "%d %d %d", sizeof(str1), sizeof(str2), sizeof("abcd" ) ); }
A
4 4 5
B
4 4 4
C
4 5 5
D
4 5 4
Soln.
Ans: C
Ans: C
In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable.In second sizeof the name str2 indicates the name of the array whose size is 5(including the '\0' termination character). The third sizeof is similar to the second one.
My C/C++ Videos on Youtube
Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.