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
What is the output ?
int main() { int a = 2; printf ("Hello" + a); return 0; }
A
Helloa
B
Hello+a
C
llo
D
None of the above
Soln.
Ans: C
Ans: C
The above code is equivalent to the following
int main() { int a = 2; char *k = "Hello"; printf (k + a); return 0; }So k + a ⇒ k + 2 which points to "llo" within "Hello" and therefore llo is printed.
Question 2
What is the output of the following program ?
void main() { char *p; // ASSUME THAT A char occupies 1 byte // and addresses are 32 bit printf("%d %d ",sizeof(*p),sizeof(p)); }
A
Compiler Error
B
1 3
C
1 4
D
4 1
Soln.
Ans: C
Ans: C
The sizeof() operator gives the number of bytes takenby its operand. P is a character pointer, which needs onebyte for storing its value (a character). Hence sizeof(*p)gives a value of 1. Since it needs 4 bytes to store theaddress of an address or pointer, sizeof(p) gives 4.
Question 3
What is the output?
int main() { char name = 'hello'; printf ("%c", name); return 0; }
A
h
B
hello
C
Compiler Error
D
o
Soln.
Ans: C
Ans: C
The string 'hello' is illegal for a char.
Question 4
How many times does the following loop execute ?
int main() { while (9) { } return 0; }
A
forever
B
8
C
9
D
never
Soln.
Ans: A
Ans: A
The loop executes for ever.
Question 5
How many times does the following loop execute ?
int main() { do { continue; }while (false); return 0; }
A
forever
B
never
C
2
D
1
Soln.
Ans: D
Ans: D
The loop executes once.
Question 6
What is the output?
int main () { int x = 1; switch (x) { case 0: printf ("A"); break; case (int)1.4: printf ("B"); break; case 2: printf ("C"); break; default: printf ("D"); } return 0; }
A
A
B
B
C
C
D
D
Soln.
Ans: B
Ans: B
The
case (int)1.4:is same as
case 1:because 1.4 is cast as an int.
Question 7
What will be output if you will compile and execute the following c code?
void main() { if(printf("c/")) { printf("c++"); } else { printf("d"); } }
A
c/d
B
c++
C
c/c++
D
d
Soln.
Ans: C
Ans: C
Return type of printf function is integer which returns number of character it prints including blank spaces. So printf function inside if condition will return 2. In if condition any non- zero number means true so else part will not execute.
Question 8
Which code is correct ?
void afunc(char* c) { } /*code 1*/ int main () { int i = 0; afunc ("c"); return 0; } /*code 2*/ int main () { int i = 0; afunc (i); return 0; } /*code 3*/ int main () { int i = 0; afunc ('c'); return 0; }
A
Code 2
B
Code 1 and Code 3 both are correct
C
Code 3
D
Code 1
Soln.
Ans: D
Ans: D
Code 2 and Code 3 are wrong because they don't pass the argument as char*.
Question 9
How many times does the following loop execute ?
int main() { while (1) { break; } return 0; }
A
1
B
forever
C
2
D
never
Soln.
Ans: A
Ans: A
The loop executes once.
Question 10
Which line below should need a pre-validation if this code should never fail ?
int main () { /* line 1 */ int iAge; /* line 2 */ cout << "Enter you age ?"; /* line 3 */ cin >> iAge; /* line 4 */ cout << "Your age is: " << iAge << endl; }
A
Line 2
B
Line 3
C
Line 1
D
Line 4
Soln.
Ans: B
Ans: B
Line 3 can be a source of error because if the user enters a very large number, or doesnot enter a number at all.
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.