0% found this document useful (0 votes)
1 views

Programming Fundamentals Quiz

quiz

Uploaded by

nokawel707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Programming Fundamentals Quiz

quiz

Uploaded by

nokawel707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Programming Fundamentals quiz 1

1. What will be the output of the following C++ code?


int a = 5, b = 10, c;
c = ++a * b-- - --a;
cout << c;
 50
 45
 55
 60
Answer: C) 55

---

2. What is the output of the following code?


int x = 10, y = 20;
int result = (x < y) ? ++x : y--;
cout << x << " " << y << " " << result;
 10 19 11
 11 20 11
 11 19 11
 10 20 20
Answer: C) 11 19 11

3. What will be the output of this program?


int a = 3, b = 2, c = 1;
int result = (a > b) ? (b > c) ? a : b : c;
cout << result;
 1
 2
 3
 Compilation error
Answer: C) 3

4. Which of the following are valid reasons for a segmentation fault in C++?
 Dereferencing a null pointer
 Accessing an array out of bounds
 Infinite recursion
 All of the above
Answer: D) All of the above

5. Given the following C++ program, what will be the output?


int a = 7;
if (a++ > 7)
cout << "Hello ";
else if (a-- < 8)
cout << "World ";
else
cout << "!";
 Hello
 World
 !
 No output
Answer: B) World

6. Which of the following is true for switch statements in C++ when multiple
cases have the same label?
 The program will compile and execute normally.
 The program will compile but throw a runtime error.
 The program will not compile due to duplicate labels.
 The program will enter an infinite loop.
Answer: C) The program will not compile due to duplicate labels.

7. What will be the output of this code involving mixed unary and binary
operators?
int x = 5;
int y = --x * x++ + x;
cout << y;
 24
 25
 26
 27
Answer: C) 26

8. Which of the following is the correct behavior of the following code?


int a = 10;
int b = a++ + ++a;
cout << b;
 20
 22
 21
 Undefined behavior
Answer: D) Undefined behavior

9. Which of the following errors will occur when dividing by zero in C++ with
an integer variable?
 Syntax error
 Compilation error
 Runtime error
 Undefined behavior
Answer: C) Runtime error

10. What will be the output of the following C++ program?


int x = 5, y = 10;
x = y++ + --x - y--;
cout << x << " " << y;
 5 10
 4 10
 49
 69
Answer: C) 4 9

11. Which of the following conditions will lead to an infinite loop in C++?
 Using while(1) without a break
 Forgetting to increment a loop counter
 Using for loop without an update expression
 All of the above
Answer: D) All of the above

12. What is the result of the following code?


int x = 5, y = 2;
int z = ++x * y-- - --x / y;
cout << z;
 10
 12
 14
 Undefined behavior
Answer: B) 12
13. Which of the following is true for the switch statement?
 switch can work with integer and char types only.
 switch cases are evaluated using && operators internally.
 The default case is mandatory in every switch statement.
 switch cases are evaluated sequentially, regardless of their order.
Answer: A) switch can work with integer and char types only.

14. What will the following code print?


int x = 10;
if (++x == 11 && x-- == 11)
cout << "A";
else
cout << "B";
cout << x;
 A10
 A11
 B10
 B11
Answer: A) A10

15. What happens if you use a float expression in a switch statement in C++?
 The code will compile and run, treating float as int.
 The code will not compile due to type incompatibility.
 The program will throw a runtime error.
 The program will run, but the switch will be ignored.
Answer: B) The code will not compile due to type incompatibility.

16. What is the output of the following program with mixed conditional
operators?
int x = 5, y = 10, z = 15;
int result = (x < y && y > z) ? y : z;
cout << result;
 5
 10
 15
 0
Answer: C) 15

17. What will be printed by the following C++ program?


int a = 5, b = 10, c = 15;
if (a > b)
if (b > c)
cout << a;
else
cout << c;
else
cout << b;
 5
 10
 15
 Nothing (compilation error)
Answer: B) 10

18. Which of the following is true about switch fall-through in C++?


 Only default can fall through to the next case.
 All cases fall through unless a break is used.
 C++ prevents fall-through between switch cases.
 Fall-through is only allowed in certain compilers.
Answer: B) All cases fall through unless a break is used.

19. What will be the output of this code?


int x = 3;
int y = x++ + ++x + --x + x--;
cout << y;
 12
 11
 10
 13
Answer: A) 12

20. Which of the following is true for both ++ and -- operators in C++?
 They can only be used with integer types.
 They can only be used as prefix operators.
 They modify the operand directly.
 They cannot be used within conditional expressions.
Answer: C) They modify the operand directly.

You might also like