C++ Set 3 (30 mcqs)

1. How many ways of passing a parameter are there in c++?
a) 1
b) 2
c) 3
d) 4

2. Which is used to keep the call by reference value as intact?
a) static
b) const
c) absolute
d) none of the mentioned

3. By default how the value are passed in c++?
a) call by value
b) call by reference
c) call by pointer
d) none of the mentioned

4. What is the output of this program?
1.#include <iostream>
2.using namespace std;
3.void copy (int& a, int& b, int& c)
4.{
5.    a *= 2;
6.    b *= 2;
7.    c *= 2;
8.}
9.int main ()
10.{
11.    int x = 1, y = 3, z = 7;
12.    copy (x, y, z);
13.    cout << “x =” << x << “, y =” << y << “, z =” << z;
14.    return 0;
15.}
a) 2 5 10
b) 2 4 5
c) 2 6 14
d) none of the mentioned

5. What is the new value of x?
1.#include <iostream>
2.using namespace std;
3.void fun(int &x)
4.{
5.    x = 20;
6.}
7.int main()
8.{
9.     int x = 10;
10.     fun(x);
11.     cout << “New value of x is ” << x;
12.     return 0;
13.}
a) 10
b) 20
c) 15
d) none of the mentioned

6. What is the output of this program?
1.#include <iostream>
2.using namespace std;
3.long factorial (long a)
4.{
5.    if (a > 1)
6.        return (a * factorial (a + 1));
7.    else
8.        return (1);
9.}
10.int main ()
11.{
12.    long num = 3;
13.    cout << num << “! = ” << factorial ( num );
14.    return 0;
15.}
a) 6
b) 24
c) segmentation fault
d) compile time error

7. What is the output of this program?
1.#include <iostream>
2.using namespace std;
3.void square (int *x)
4.{
5.        *x = (*x + 1) * (*x);
6.}
7.int main ( )
8.{
9.        int num = 10;
10.    square(&num);
11.    cout << num;
12.    return 0;
13.}
a) 100
b) compile time error
c) 144
d) 110

8. What is the output of this program?
1.#include <iostream>
2.using namespace std;
3.int add(int a, int b);
4.int main()
5.{
6.    int i = 5, j = 6;
7.    cout << add(i, j) << endl;
8.    return 0;
9.}
10.int add(int a, int b )
11.{
12.    int sum = a + b;
13.    a = 7;
14.    return a + b;
15.}
a) 11
b) 12
c) 13
d) compile time error

9. What will happen when we use void in argument passing?
a) It will not return value to its caller
b) It will return value to its caller
c) both a & b are correct
d) none of the mentioned

10. What is the output of this program?
1.#include <iostream>
2.using namespace std;
3.void Sum(int a, int b, int & c)
4.{
5.    a = b + c;
6.    b = a + c;
7.    c = a + b;
8.}
9.int main()
10.{
11.    int x = 2, y =3;
12.    Sum(x, y, y);
13.    cout << x << ” ” << y;
14.    return 0;
15.}
a) 2 3
b) 6 9
c) 2 15
d) compile time error

11. To where does the program control transfers when exception is arised?
a) catch
b) handlers
c) throw
d) none of the mentioned

12. Which key word is used to check exception in the block of code?
a) catch
b) throw
c) try
d) none of the mentioned

13. What will happen when the exception is not caught in the program?
a) error
b) program will execute
c) block of that code will not execute
d) none of the mentioned

14. What is the output of this program?
1.#include <iostream>
2.using namespace std;
3.int main()
4.{
5.    int age = 0;
6.    try {
7.        if (age < 0) {
8.            throw “Positive Number Required”;
9.        }
10.        cout << age;
11.    }
12.    catch(const char *Message)
13.    {
14.        cout << “Error: ” << Message;
15.    }
16.    return 0;
17.}
a) 0
b) Error:Positive Number Required
c) compile time error
d) none of the mentioned

15. What is the output of this program?
1.#include <iostream>
2.using namespace std;
3.void PrintSequence(int StopNum)
4.{
5.    int Num;
6.    Num = 1;
7.    while (true) {
8.        if (Num >= StopNum)
9.            throw Num;
10.        cout << Num;
11.        Num++;
12.    }
13.}
14.int main(void)
15.{
16.    try {
17.        PrintSequence(20);
18.    }
19.    catch(int ExNum)
20.    {
21.        cout << “Caught an exception with value: ” << ExNum;
22.    }
23.    return 0;
24.}
a) compile time error
b) prints first 19 numbers
c) prints first 19 numbers and throws exception at 20
d) none of the mentioned

16. What is the output of this program?
1.#include <iostream>
2.using namespace std;
3.double division(int a, int b)
4.{
5.    if (b == 0) {
6.        throw “Division by zero condition!”;
7.    }
8.    return (a / b);
9.}
10.int main ()
11.{
12.    int x = 50;
13.    int y = 2;
14.    double z = 0;
15.    try {
16.        z = division(x, y);
17.        cout << z;
18.    }
19.    catch(const char *msg) {
20.        cerr << msg;
21.    }
22.    return 0;
23.}
a) 25
b) 20
c) Division by zero condition!
d) none of the mentioned

17. What is the output of this program?
1.#include <iostream>
2.using namespace std;
3.int main()
4.{
5.    char* buff;
6.    try {
7.        buff = new char[1024];
8.        if (buff == 0)
9.           throw “Memory allocation failure!”;
10.        else
11.           cout << sizeof(buff) << “Byte successfully allocated!”<<endl;
12.    }
13.    catch(char *strg) {
14.        cout<<“Exception raised: “<<strg<<endl;
15.    }
16.    return 0;
17.}
a) 4 Bytes allocated successfully
b) 8 Bytes allocated successfully
c) Memory allocation failure
d) depends on the size of data type

18. What is the output of this program?
1.#include <iostream>
2.using namespace std;
3.void Funct();
4.int main()
5.{
6.    try {
7.        Funct();
8.    }
9.    catch(double) {
10.        cerr << “caught a double type…” << endl;
11.    }
12.    return 0;
13.}
14.void Funct()
15.{
16.    throw 3;
17.}
a) caught a double type
b) compile time error
c) abnormal program termination
d) none of the mentioned

19. What is the output of this program?
1.#include <iostream>
2.#include <exception>
3.using namespace std;
4.int main()
5.{
6.    try {
7.        int * array1 = new int[100000000];
8.        int * array2 = new int[100000000];
9.        int * array3 = new int[100000000];
10.        int * array4 = new int[100000000];
11.        cout << “Allocated successfully”;
12.    }
13.    catch(bad_alloc&) {
14.        cout << “Error allocating the requested memory.” << endl;
15.    }
16.    return 0;
17.}
a) Allocated successfully
b) error allocating the requested memory
c) Depends on the memory of the computer
d) none of the mentioned

20. What will happen when the handler is not found for exception?
a) Calls the standard library function terminate()
b) raise an error
c) executes the remaining block
d) none of the mentioned

21. Which operator works only with integer variables?
a) increment
b) decrement
c) both a & b
d) None of the mentioned

22. How many types are there in increment/decrement operator?
a) 1
b) 2
c) 3
d) 4

23. Pick out the correct statement.
a) Increment operator ++ adds 1 to its operand
b) Increment operator ++ adds 2 to its operand
c) Decrement operator ++ subtracts 1 to its operand
d) None of the mentioned

24. What is the output of this program?
1.#include <stdio.h>
2.using namespace std;
3.int main()
4.{
5.    int a = 21;
6.    int c ;
7.    c = a++;
8.    cout << c;
9.    return 0;
10.}
a) 21
b) 22
c) 23
d) 20

25. What is the output of this program?
1.#include <stdio.h>
2.using namespace std;
3.int main()
4.{
5.    int x = 5, y = 5;
6.    cout << ++x << –y << endl;
7.    return 0;
8.}
a) 55
b) 64
c) 46
d) 45

26. What is the output of this program?
1.#include <stdio.h>
2.using namespace std;
3.int main()
4.{
5.    int x = 5, y = 5, z;
6.    x = ++x; y = –y;
7.    z = x++ + y–;
8.    cout << z;
9.    return 0;
10.}
a) 10
b) 11
c) 9
d) 12

27. What is the output of this program?
1.#include <stdio.h>
2.using namespace std;
3.int main()
4.{
5.    int x = 5, y = 5, z;
6.    x = ++x; y = –y;
7.    z = x + ++x;
8.    cout << z;
9.    return 0;
10.}
a) 11
b) 12
c) 13
d) 14

28. What is the output of this program?
1.#include <stdio.h>
2.using namespace std;
3.int main()
4.{
5.    int num1 = 5;
6.    int num2 = 3;
7.    int num3 = 2;
8.    num1 = num2++;
9.    num2 = –num3;
10.    cout << num1 << num2 << num3;
11.    return 0;
12.}
a) 532
b) 235
c) 312
d) 311

29. Pick out the correct statement
a) Preincrement is faster than postincrement.
b) postincrement is faster than preincrement.
c) Both a & b
d) None of the mentioned

30. Which concepts does the preincrement uses?
a) call by value
b) call by reference
c) queue
d) None of the mentioned

Answers

1-c 2-b 3-a 4-c 5-b
6-c 7-d 8-c 9-a 10-c
11-b 12-c 13-a 14-a 15-c
16-a 17-d 18-c 19-c 20-a
21-c 22-b 23-a 24-a 25-b
26-a 27-d 28-d 29-a 30-b
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *