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

Exit exam- Computer programming questions

The document contains a series of programming questions related to C++ code snippets, including outputs of code, loop execution counts, structure definitions, and function prototypes. Each question is followed by multiple-choice answers. The questions cover various concepts such as references, arrays, loops, and conditional statements.

Uploaded by

hibutefera
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Exit exam- Computer programming questions

The document contains a series of programming questions related to C++ code snippets, including outputs of code, loop execution counts, structure definitions, and function prototypes. Each question is followed by multiple-choice answers. The questions cover various concepts such as references, arrays, loops, and conditional statements.

Uploaded by

hibutefera
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What is the output of the following code snippet?

#include <iostream>
using namespace std;
int main() {
int x = 10;
int& ref = x;
int* ptr = &ref;
*ptr = 20;
cout << x << endl;
return 0;
}
a) 10
b) 20
c) Compiler error
d) Runtime error
2. What is the output of the following code snippet?
#include <iostream>
using namespace std;
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5;
int y = 3;
swap(x, y);
cout << x << " " << y;
return 0;
}
a) 5 3
b) 3 5
c) 5 5
d) Error
3. How many times will the following loop execute?
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
cout << i << " ";
}
a) 4
b) 5
c) 9
d) 10
4. What will be the output of the following code snippet?
#include <iostream>
using namespace std;
void modifyArray(int arr[]) {
arr[0] = 10;
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
modifyArray(numbers);
cout << numbers[0];
return 0;
}
a) 1
b) 2
c) 3
d) 10
5. What will be the output of the following code snippet?
#include <iostream>
using namespace std;
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5); i++) {
sum += numbers[i];
}
cout << sum;
return 0;
}
a) 1
b) 2
c) 15
d) 10
6. Which of the following correctly defines a structure named "Person" with two member variables:
"name" and "age"?
a)
struct Person {
string name;
int age;
};
b)
class Person {
public:
string name;
int age;
};
c)
typedef struct {
string name;
int age;
} Person;
d)
object Person {
string name;
int age;
};
7. How many times will the following loop execute?
int i = 0;
while (i < 5) {
cout << i << " ";
i += 2;
}
a) 3
b) 4
c) 5
d) 6
8. What will be the output of the following code snippet?
#include <iostream>
using namespace std;
void printEvenNumbers(int arr[], int size) {
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
cout << arr[i] << " ";
}
}
}
int main() {
int numbers[] = {1, 2, 3, 4, 5, 6};
printEvenNumbers(numbers, 6);
return 0;
}
a) 2 4 6
b) 1 3 5
c) 1 2 3 4 5 6
d) 0 0 0 0 0 0
9. What is the purpose of a function prototype in C++?
a) To define the body of a function
b) To declare the existence of a function
c) To specify the return type of a function
d) To provide the input parameters for a function
10. Which loop structure is suitable when the number of iterations is known in advance?
a) for loop
b) while loop
c) do-while loop
d) switch statement
11. Which array declaration is not correct
a) int numbers[3];
b) int numbers[5] = {2, 0, 5};
c) int numbers[ ] = [2, 0, 5];
d) None
12. One of the following is NOT true
a) when the result of condition is true in if else, the body of the else statement is never executed
b) when result of condition is false, the body of the if statement is executed
c) case keyword is used to identify possible entry points of switch statement
d) In do while, its body is executed first and then the loop condition is examined
e) If expression 2 is omitted in for loop, C++ assumes that continuation-test condition is true which
result forever loop
13. What is the output of the following program?
#include < iostream >
using namespace std;
void fun(int x, int y)
{
x = 20;
y = 10;
}
int main()
{
int x = 10;
fun(x, x);
cout << x;
return 0;
}
a) 10
b) 20
c) compile time error
d) none

14. What is the scope of a variable which is declared in the user-defined function?
a) whole program (global)
b) only inside the {} block
c) both a and b
d) none
15. What should be the output of the code below?
#include<iostream>
using namespace std;
int main()
{
Int a=5;
Int b=6;
Int c;
c= (a>b) ? a : b;
cout<<c;
return 0;
}
a) 5
b) 4
c) 7
d) 6

You might also like