Worksheetsjustjustgig
Total questions: 40
Worksheet time: 20mins
Name
Class
Date
1.
Determine the output: int a[5] = {9, 7, 5, 3, 1}; int x = 0; for(int i = 0; i < 5; i++) x += a[i] * (i % 2); cout << x;
a)
10
b)
12
c)
14
d)
16
2.
Evaluate the result: int a[6] = {4, 7, 10, 13, 16, 19}; int r = 0; for(int i = 0; i < 6; i++) if(a[i] % 3 == 1) r += i; cout << r;
a)
6
b)
9
c)
12
d)
15
3.
Find the output: int arr[5] = {100, 80, 60, 40, 20}; for(int i = 0; i < 4; i++) if(arr[i] > arr[i+1]) arr[i] = arr[i+1]; cout << arr[0];
a)
20
b)
40
c)
60
d)
80
4.
Find the output: int m[3][3] = {{9,8,7},{6,5,4},{3,2,1}}; int s = 0; for(int i = 0; i < 3; i++) s += m[i][i] + m[i][2-i]; cout << s;
a)
25
b)
30
c)
35
d)
40
5.
Find the result: int m[2][4] = {{5,10,15,20},{25,30,35,40}}; int r = 0; for(int i = 0; i < 2; i++) for(int j = 0; j < 4; j++) if((i+j) % 2 == 0) r += m[i][j]; cout << r;
a)
60
b)
80
c)
100
d)
120
6.
How many times will "Hello" be printed? int i = 0; while(i < 3) { cout << "Hello "; i++; }
a)
2
b)
3
c)
4
d)
0
7.
What is the final output? int arr[] = {10, 20, 30, 40, 50}; int *p1 = arr + 1; int *p2 = arr + 3; int *p3 = arr + 2; *p1 += *p2; *p3 += *p1; *(p2 - 1) *= 2; cout << arr[1] << " " << arr[2] << " " << *(arr + 3);
a)
60 120 40
b)
60 90 40
c)
60 150 40
d)
50 120 40
8.
What is the final output? void modify(int* p, int n) { for(int i = 0; i < n; i++) p[i] = p[i] * 2 + i; } int transform(int arr[], int size, void (*f)(int*, int)) { f(arr, size); int sum = 0; for(int i = 0; i < size; i++) sum += arr[i]; return sum; } int data[] = {1, 2, 3, 4}; int result = transform(data, 4, modify); cout << result << " " << data[2];
a)
26 7
b)
26 6
c)
28 7
d)
28 6
9.
What is the final value of result? int apply(int (*func)(int, int), int a, int b) { return func(a, b); } int add(int x, int y) { return x + y; } int mult(int x, int y) { return x * y; } int result = apply(mult, apply(add, 3, 5), apply(add, 2, 4));
a)
48
b)
56
c)
64
d)
72
10.
What is the output? auto lambda = [](auto... args) { return (args + ...); }; int a = lambda(1, 2, 3); double b = lambda(1.5, 2.5, 3.0, 4.0); cout << a + (int)b;
a)
17
b)
18
c)
19
d)
20
11.
What is the output? char str[] = "HELLO"; char *p = str; char **pp = &p; (*p)++; p++; (*p)++; cout << str << " " << *pp;
a)
IELLO HELLO
b)
IFLЛО ELLO
c)
IFLLO IFLLO
d)
HELLO ELLO
12.
What is the output? constexpr int power(int base, int exp) { return (exp == 0) ? 1 : base * power(base, exp - 1); } template<int N> int generate() { return power(2, N) + generate<N-1>(); } template<> int generate<0>() { return 1; } int main() { cout << generate<4>(); return 0; }
a)
15
b)
16
c)
30
d)
31
13.
What is the output? for(int i = 0; i < 4; i += 2) { cout << i << " "; }
a)
0 2
b)
0 2 4
c)
2 4
d)
0 1 2 3
14.
What is the output? int a[5] = {50, 40, 30, 20, 10}; int s = 0; for(int i = 0; i < 5; i++) s += (a[i] / 10) * i; cout << s;
a)
30
b)
40
c)
50
d)
60
15.
What is the output? int sum = 0; for(int i = 1; i <= 4; i++) { sum += i; } cout << sum;
a)
4
b)
10
c)
5
d)
1
16.
What is the output? struct Nested { int x; struct Inner { int *ref; int val; } inner; }; void transform(Nested *n) { n->inner.val += *(n->inner.ref); *(n->inner.ref) *= 2; n->x = n->inner.val + *(n->inner.ref); } int global = 5; Nested obj = {10, {&global, 15}}; transform(&obj); transform(&obj); cout << global << " " << obj.x << " " << obj.inner.val;
a)
20 60 35
b)
20 70 35
c)
10 30 20
d)
20 50 25
17.
What is the correct syntax for an if statement in C++?
a)
if (condition) { }
b)
if condition { }
c)
if [condition] { }
d)
if {condition} ( )
18.
Which operator is used to check if two values are equal in C++?
a)
=
b)
==
c)
!=
d)
===
19.
What is the result? struct Complex { int *ptr; int value; }; void process(Complex &c1, Complex &c2) { *(c1.ptr) += c2.value; c2.value = *(c1.ptr); *(c2.ptr) *= 2; } int a = 5, b = 10; Complex x = {&a, 15}; Complex y = {&b, 20}; process(x, y); process(y, x); cout << a << " " << b << " " << x.value << " " << y.value;
a)
25 50 15 25
b)
25 70 15 45
c)
20 40 15 20
d)
30 60 15 30
20.
What will be the output of the following code?
int x = 10;
if (x > 5) {
cout << "Hello";
} else {
cout << "World";
}
a)
Hello
b)
World
c)
HelloWorld
d)
No output
21.
Which statement is used to check multiple conditions in C++?
a)
switch-case
b)
if-else if-else
c)
Both A and B
d)
None of the above
22.
What will be printed? int a[] = {1, 2, 3, 4, 5}; int *p = &a[2]; int *q = a; cout << *(p - 1) << " " << *(q + 3) << " " << p - q;
a)
2 4 2
b)
1 4 3
c)
2 3 2
d)
3 5 1
23.
What will be printed? int a[5] = {25, 20, 15, 10, 5}; int x = a[0]; for(int i = 1; i < 5; i++) if(a[i] < x) x = a[i]; cout << x * 2;
a)
5
b)
10
c)
15
d)
20
24.
What will be printed? int a[6] = {11, 22, 33, 44, 55, 66}; int s = 0; for(int i = 0; i < 6; i += 2) s += a[i] / 11; cout << s;
a)
6
b)
9
c)
12
d)
15
25.
What will be printed? int a[8] = {2, 4, 8, 16, 32, 64, 128, 256}; int c = 0; for(int i = 1; i < 8; i++) if(a[i] == a[i-1] * 2) c++; cout << c;
a)
5
b)
6
c)
7
d)
8
26.
What will be printed? int arr[4][4]; for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) arr[i][j] = (i+1) * (j+1); cout << arr[2][3];
a)
6
b)
8
c)
10
d)
12
27.
What will be printed? int memoized_fib(int n, int memo[] = nullptr) { static int static_memo[100] = {0}; if(memo == nullptr) memo = static_memo; if(n <= 1) return n; if(memo[n] != 0) return memo[n]; memo[n] = memoized_fib(n-1, memo) + memoized_fib(n-2, memo); return memo[n]; } cout << memoized_fib(10) << " " << memoized_fib(5);
a)
55 5
b)
55 8
c)
89 5
d)
89 8
28.
What will be printed? struct Data { int arr[3]; int *p; }; Data d1 = {{1, 2, 3}, d1.arr + 1}; Data d2 = d1; d2.p = d2.arr; *(d1.p) += 5; d2.arr[0] += 10; *(d2.p + 2) *= 2; cout << d1.arr[1] << " " << d2.arr[0] << " " << *(d2.p + 2);
a)
7 11 6
b)
2 11 6
c)
7 1 6
d)
2 1 3
29.
Which header file is required to use strings in C++?
a)
<iostream>
b)
<string>
c)
<cstring>
d)
<stdio.h>
30.
What will be the output of this code? template<typename T> T mystery(T a, T b, T c) { return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c); } int x = 5; double y = mystery(3.5, mystery(7.2, 1.8, 9.3), 4.6); cout << (int)y;
a)
4
b)
7
c)
9
d)
10
31.
What will be the output? for(int i = 1; i <= 5; i++) { cout << i << " "; }
a)
1 2 3 4 5
b)
1 2 3 4
c)
0 1 2 3 4 5
d)
2 3 4 5 6
32.
Which function is used to find the length of a string in C++?
a)
size()
b)
length()
c)
count()
d)
Both A and B
33.
What will be the output? int matrix[2][3] = {{1,2,3}, {4,5,6}}; int (*p)[3] = matrix; cout << *(*p + 1) << " " << *(*(p + 1) + 2) << " " << **(p + 1);
a)
2 6 4
b)
1 5 3
c)
2 5 4
d)
3 6 4
34.
What will be the output? struct Node { int data; Node* next; }; void modify(Node** head) { (*head)->data += 10; *head = (*head)->next; (*head)->data *= 2; } Node n3 = {30, nullptr}; Node n2 = {20, &n3}; Node n1 = {10, &n2}; Node* ptr = &n1; modify(&ptr); cout << n1.data << " " << n2.data << " " << ptr->data;
a)
20 40 40
b)
10 40 40
c)
20 20 40
d)
10 20 30
35.
What will be the output of the following code?
string str = "Hello";
cout << str[0];
a)
H
b)
Hello
c)
h
d)
0
36.
What will this code print? int recursive_sum(int n, int acc = 0) { return (n == 0) ? acc : recursive_sum(n & (n-1), acc + __builtin_popcount(n & ~(n-1))); } cout << recursive_sum(15) << " " << recursive_sum(255);
a)
4 8
b)
15 255
c)
4 4
d)
8 16
37.
What will this code print? int x = 10, y = 20; int *arr[2] = {&x, &y}; int **p = arr; *(*p)++ = 30; (*p)++; cout << x << " " << y << " " << **p;
a)
30 20 20
b)
10 20 20
c)
30 20 30
d)
10 30 30
38.
What will this code print? void complex(int *p, int **pp, int ***ppp) { **pp += ***ppp; *p *= 2; ***ppp -= 5; } int x = 10; int *p1 = &x; int **p2 = &p1; int ***p3 = &p2; complex(&x, &p1, &p2); cout << x << " " << *p1 << " " << **p2 << " " << ***p3;
a)
40 40 40 40
b)
35 35 35 35
c)
30 30 30 30
d)
25 25 25 25
39.
What will this print? int count = 5; do { cout << count << " "; count--; } while(count > 3);
a)
5 4 3
b)
5 4
c)
4 3
d)
5
40.
What will this print? void strange(int &a, int &&b) { a += b; b *= 2; } int x = 10; strange(x, 5); int y = 3; strange(x, y + 2); cout << x;
a)
20
b)
22
c)
25
d)
Compilation error
100 %
