C++ Coding MCQ Questions and Answers

Welcome to C++ Programming Coding MCQ Questions. Here, we present 20 MCQs (Multiple-Choice Questions) with code snippets to test your understanding of C++ programming coding and logical stills. Let's get started!

1. What is the output of the following C++ code?

#include <iostream>
int main() {
    std::cout << "Hello, world!" << std::endl;
}
a) Hello, world!
b) Hello, world
c) Compilation error
d) Runtime error

2. What will the following C++ program output?

#include <iostream>
int main() {
    int a = 5;
    std::cout << a++;
}
a) 5
b) 6
c) Compilation error
d) Undefined behavior

3. What is the output of this C++ code?

#include <iostream>
int main() {
    for(int i=0; i<3; i++) {
        std::cout << i;
    }
}
a) 012
b) 123
c) 0123
d) Compilation error

4. Consider the following C++ program. What will it output?

#include <iostream>
int main() {
    int x = 10;
    int y = x--;
    std::cout << x << " " << y;
}
a) 9 10
b) 10 9
c) 10 10
d) 9 9

5. What does the following C++ program print?

#include <iostream>
int main() {
    int nums[] = {1, 2, 3, 4, 5};
    std::cout << nums[3];
}
a) 3
b) 4
c) 5
d) Error

6. What will this C++ program output?

#include <iostream>
int main() {
    int i = 1;
    std::cout << i++;
}
a) 0
b) 1
c) 2
d) Error

7. What is printed by the following C++ code?

#include <iostream>
int main() {
    int i = 10;
    if(i == 10) std::cout << "i is ten";
    else std::cout << "i is not ten";
}
a) i is ten
b) i is not ten
c) Compilation error
d) No output

8. What does the following C++ code output?

#include <iostream>
int main() {
    int i;
    for(i = 0; i < 5; i++);
    std::cout << i;
}
a) 0
b) 4
c) 5
d) Compilation error

9. What will the following C++ code snippet output?

#include <iostream>
int main() {
    int x = 5, y = 3;
    std::cout << x / y;
}
a) 1.67
b) 1
c) 2
d) Error

10. What does the following C++ program output?

#include <iostream>
int main() {
    int a = 1, b = 2;
    if(a-- > 0 && b++ > 2) std::cout << "Yes";
    else std::cout << "No";
    std::cout << " " << a << " " << b;
}
a) Yes 0 3
b) No 0 3
c) No 0 2
d) Yes 0 2

11. What will this code snippet print?

#include <iostream>
int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *ptr = arr;
    std::cout << *(ptr+3);
}
a) 10
b) 20
c) 40
d) Error

12. What will the following C++ code output?

#include <iostream>
int main() {
    char c = 'a';
    std::cout << char(c - 32);
}
a) A
b) a
c) z
d) Error

13. What is the output of the following C++ program?

#include <iostream>
int main() {
    int i = 0;
    while(i < 3) {
        std::cout << i << " ";
        i++;
    }
}
a) 0 1 2
b) 1 2 3
c) 0 1 2 3
d) Infinite loop

14. Analyze the following code snippet. What will it output?

#include <iostream>
int main() {
    int x = 5;
    int y = x * 10;
    std::cout << y;
}
a) 5
b) 10
c) 50
d) 100

15. What will the following C++ program output?

#include <iostream>
int main() {
    int i = 10;
    if(i == 10)
    std::cout << "i is ten\n";
    std::cout << "i is definitely ten\n";
}
a) i is ten
b) i is definitely ten
c) i is ten
d) Compilation error

16. What is the output of this C++ code?

#include <iostream>
int main() {
    int num = 4;
    std::cout << num++;
}
a) 3
b) 4
c) 5
d) 6

17. Consider the following C++ program. What will it print?

#include <iostream>
int main() {
    for(int i = 1; i <= 5; i++) {
        if(i % 2 == 0) continue;
        std::cout << i << " ";
    }
}
a) 1 3 5
b) 1 2 3 4 5
c) 2 4
d) None

18. What does the following C++ program output?

#include <iostream>
int main() {
    int a = 5, b = 0;
    std::cout << a / b;
}
a) 0
b) 5
c) Runtime error
d) Undefined

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

#include <iostream>
int main() {
    int x = 10, y = 5;
    std::cout << x % y;
}
a) 0
b) 5
c) 10
d) None of the above

20. What does this code snippet output?

#include <iostream>
int main() {
    int i = 3;
    while(i--) {
        std::cout << i << " ";
    }
}
a) 3 2 1
b) 2 1 0
c) 2 1
d) 1 0

Comments