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

CPP_MTE_Questions

mte questions

Uploaded by

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

CPP_MTE_Questions

mte questions

Uploaded by

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

MTC-101 MTE 2024

Section 1 (20 X 0.5 = 10 Marks)

1. What is the output of the following code?

int i = 1;
for(; i <= 5; i++) {
if (i % 2 == 0) continue;
cout << i << ' ';
}
A) 1 3 5
B) 1 2 3 4 5
C) 1 2 4
D) 2 4
E) Compilation error

2. What happens if the condition in a `while` loop is always true?


A) The loop will never execute
B) The loop will run indefinitely
C) The loop will terminate after the first iteration
D) The loop will throw a runtime error
E) The program will compile but may hang

3. Which of the following for loops is equivalent to the while loop below?
int i = 0;
while(i < 5) {
cout << i << ' ';
i++;
}

A) for (int i = 0; i <= 5; i++) { cout << i << ' '; }


B) for (int i = 0; i < 5; i++) { cout << i << ' '; }
C) for (int i = 1; i < 5; i++) { cout << i << ' '; }
D) for (int i = 0; i < 5; i++) { cout << i; }
E) for (int i = 1; i <= 5; i++) { cout << i; }

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


for(int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++) {
if(j == 1) break;
cout << j << ' ';
}
}

A) 0 1 0 1 0 1
B) 0 0 0
C) 0 1 0 1
D) 0 0
E) Compilation error

5. How many times will the following loop execute?


int i = 5;
while (i--) {
cout << i << ' ';
}
A) 5 times
B) 6 times
C) 4 times
D) Infinite loop
E) 0 times (because of decrementing)

6. What is the output of the following code?


int function();
int main(){
int i;
i = function();
printf("%d",i);
return 0;
}

int function()
{
int a;
a = 250;
}
A) 10
B) 5
C) Compilation error
D) Undefined behavior
E) Segmentation fault

7. What will happen if a return statement is omitted from a non-void function in


C++?
A) The program will compile and run without any issues
B) The program will result in undefined behavior
C) The program will throw a runtime error
D) The program will not compile
E) The function will implicitly return 0

8. What is the output of the following code?


int func(int a, int b = 2, int c = 3) {
return a + b + c;
}

int main() {
cout << func(1);
}
A) 6
B) 5
C) 7
D) Compilation error
E) 1

9. What is the output of the following code?


int arr[] = {1, 2, 3, 4, 5};
cout << arr[5];
A) 0
B) 5
C) Garbage value
D) Compilation error
E) Undefined behavior

10. Which of the following correctly defines a 2D array in C++?


A) int arr[2][];
B) int arr[2][3];
C) int arr[] = {1, 2, 3};
D) int arr[2][3] = {1, 2, 3};
E) int arr[][2];

11. What is the output of the following code?


#include <iostream>
#include <cmath>
using namespace std;

double mysteriousFunction(double x, double y)


{
return floor(pow(x, sqrt(x + 2 * y)));
}

int main()
{
double x = 2, y = 2, z;
z = mysteriousFunction(x, y);
cout << z;
}
A) 2
B) 3
C) 5
D) Compilation error
E) None

12. What is the correct way to pass an array to a function in C++?


A) void func(int arr[]);
B) void func(int* arr);
C) void func(int arr[10]);
D) void func(int arr[], int size);
E) All of the above except D

13. Which of the following correctly initializes a string in C++?


A) string str = "Hello";
B) char str[] = "Hello";
C) string str[] = {'H', 'e', 'l', 'l', 'o'};
D) All of the above
E) All of the above except C

14. What is the output of the following code?


string str = "Hello";
cout << str.length();
A) 4
B) 5
C) 6
D) Compilation error
E) Undefined behavior

15. What is the output of the following code?


string str = "abc";
str += "def";
cout << str;
A) abc
B) def
C) abcdef
D) Compilation error
E) Runtime error

16. What will be the output of the following code?


int a = 5;
int b = ++a * 2 - a--;
cout << a << ' ' << b;
A) 4 12
B) 6 12
C) 5 10
D) 4 10
E) 6 10
F) None

17. Which of the following operations will result in a floating-point division


in C++?
A) int a = 5 / 2;
B) double a = 5 / 2;
C) float a = (float)5 / 2;
D) float a = 5 / 2.0;
E) Both C and D
18. What is the output of the following code?
int x = 1, y = 2;
int z = (x > y) ? (x + y) : (x - y);
cout << z;
A) -1
B) 1
C) 2
D) 3
E) Compilation error

19. What will the following code print?


int a = 1, b = 0, c = -1;
if (a && b || c)
cout << "True";
else
cout << "False";

A) True
B) False
C) Compilation error
D) Undefined behavior
E) No output

20. What is the output of the following code?


int x = 10, y = 5;
if (x = y)
cout << "Equal";
else
cout << "Not Equal";
A) Equal
B) Not Equal
C) Compilation error
D) Undefined behavior
E) Runtime error

________________________________________________________________________________

Section 2 (1.5 x 7 = 10.5 Marks)

21. If the following program does not compile, why not? If it does compile, what
is the output when it is run?

#include <iostream>
using namespace std;
int main()
{
char ch = '0';
ch++;
cout << ch << endl;
cout << (int)ch << endl;
ch += 7;
cout << ch << endl;
cout << (int)ch << endl;
int n = ch;
cout << n << endl;
cout << (char)n << endl;
int m = ch - '3';
cout << m << endl;
}

(Hint: ASCII value of character 0 is 48.)

22. If the following program does not compile, why not? If it does compile, what
is the output when it is run?

#include <iostream>
using namespace std;

int main()
{
for(int month = 1;month<3;month++)
{
switch(month)
{
case 1: cout << "Two roads diverged in a wood.";
break;
case 12: cout << "I took the one less travelled by,";
break;
case 3: cout << "and that has made all the difference.";
break;
default: cout << "?";
}
}
}

23. If the following program does not compile, why not? If it does compile, what
is the output when it is run?
#include <iostream>
using namespace std;

int main()
{
const int SIZE = 100;
int score[ SIZE ], i, min;

for(i = 0; i < SIZE ; i++)


score[ i ] = i + 60;

for(i = 0, min = 2147483647; i < SIZE ; i++)


if ( score[ i ] < min )
min = score[i];

cout << "Minimal value is: " << min << endl;
}
24. If the following program does not compile, why not? If it does compile, what
is the output when it is run?
#include <iostream>
using namespace std;

int main()
{
const int SIZE = 3;
int arr[SIZE][SIZE] = { {1,2,3}, {4,5,6}, {7,8,9} };

for(int i = 0 ; i < SIZE ; i++ )


{
for(int j = 0 ; j < SIZE ; j++ )
cout << arr[ i ][ j ];
cout << endl;
}
}
25. If the following program does not compile, why not? If it does compile, what
is the output when it is run?
#include <iostream>
using namespace std;

void arrayIntegerDivision(const int x[],int n, int y)


{
if(y == 0)
{
cout << "Cannot divide by zero!" << endl;
return ;
}

for(int i = 0 ; i < n ; i++ )


x[ i ] = x[ i ] / y;
}

int main()
{
const int SIZE = 10;
int x[ SIZE ] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
int y = 2;

arrayIntegerDivision(x, SIZE, y);

for(int i = 0; i< SIZE ; i++ )


cout << x[i] << endl;
}
26. If the following program does not compile, why not? If it does compile, what
is the output when it is run?
#include <iostream>
using namespace std;

void output(int p, int q)


{
cout << "Integer output! ";
}

void output(double p, double q)


{
cout << "Double output! ";
}

int main()
{
int x = 1, y = 1;
double a = 1.0, b = 1.0;

output(x,y);
output(a,b);
output(x,b);
output(a,x);
output(a,y);
}

27. The following program does not compile. Where are the compilation errors at?
(1 marks)
#include <iostream>
using namespace std;

int main()
{
int @__#__#__@; // (1)

int 987654321; // (2)

int X987654321; // (3)

unsigned char ten%; // (4)

long double F-16; // (5)

long double F16; // (6)

long int t.e.n; // (7)

unsigned long int ten; // (8)

float __; // (9)

int zzzzzzzzzzzzzzzzzzZZZZZ; // (10)

////////////////////////////////// // (11)

You might also like