C++ Toughest Exam Questions! Trivia

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Catherine Halcomb
C
Catherine Halcomb
Community Contributor
Quizzes Created: 1428 | Total Attempts: 5,897,934
Questions: 20 | Attempts: 226

SettingsSettingsSettings
C++ Toughest Exam Questions! Trivia - Quiz

Are you looking for some C++ toughest exam questions? It is one of the fastest programming languages to exist, and this is why it used by most gaming companies for gaming engines. This language can be challenging to learn, and the trivia quiz below is perfect for you to test out just how much you understand about it. How about you give it a shot and see how well you will do.


Questions and Answers
  • 1. 

    It's necessary to specify the variable name in the parameter list.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    In programming, it is not always necessary to specify the variable name in the parameter list. In some programming languages, such as Python, you can define a function with parameters without explicitly specifying the variable names. This is commonly known as using "anonymous" or "unpacked" parameters. However, in most programming languages, it is considered good practice to specify the variable names in the parameter list as it improves code readability and makes it easier for other programmers to understand and maintain the code. Therefore, the statement "it's necessary to specify the variable name in the parameter list" is false.

    Rate this question:

  • 2. 

    Function pow(x,y) returns a value of type int.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The given statement is false because the function pow(x,y) does not necessarily return a value of type int. The return type of the pow() function depends on the programming language being used. In some languages, such as C and C++, the pow() function returns a double or float value. Therefore, the correct answer is false.

    Rate this question:

  • 3. 

    Formal parameter: variable or expression listed in a call to a function.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    A formal parameter is not a variable or expression listed in a call to a function. It is a variable or placeholder that is used in the function definition to represent the values that will be passed into the function when it is called. The actual values that are passed into the function when it is called are called arguments. Therefore, the statement "formal parameter: variable or expression listed in a call to a function" is incorrect.

    Rate this question:

  • 4. 

    A value returning function is used in an assignment or in an input statement.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    A value returning function is not used in an assignment or in an input statement. Instead, a value returning function is used when we want to perform a calculation or operation and return a value back to the caller. Assignments and input statements are used to store or receive values, not to call functions. Therefore, the statement is false.

    Rate this question:

  • 5. 

    Formal parameters are necessary for a void function.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    False. Formal parameters are not necessary for a void function. A void function is a function that does not return a value. While formal parameters can be used in a void function to pass values into the function, they are not required. Void functions can be defined without any formal parameters.

    Rate this question:

  • 6. 

    Value parameter: a formal parameter that receives the location of the corresponding Actual parameter.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The explanation for the given answer is that the statement is incorrect. The value parameter is a formal parameter that receives the value of the corresponding actual parameter, not the location. In other words, the value of the actual parameter is copied into the value parameter during a function call.

    Rate this question:

  • 7. 

    What is the output of the following code: #include <iostream> using namespace std; int main() { int x=10; if(x <= 10) cout<<"A";  else if (x<=20) cout<<"B";  else  cout<<"C";  return 0; }

    • A.

      A

    • B.

      B

    • C.

      C

    • D.

      A B

    Correct Answer
    A. A
    Explanation
    The output of the code is "A". This is because the value of the variable x is 10, which is less than or equal to 10. Therefore, the condition in the if statement is true and the code inside the if block is executed, which prints "A" to the console.

    Rate this question:

  • 8. 

    What is the output of the following code: #include <iostream> using namespace std; int main() {      int i;  for(i=1 ; i<=5 ; i++) ;     cout<<"A";     cout<<"B";     return 0; }

    • A.

      AABB

    • B.

      B

    • C.

      AB

    • D.

      ABAB

    Correct Answer
    C. AB
    Explanation
    The code includes a for loop that runs from 1 to 5, but the body of the loop is empty due to the semicolon placed immediately after the closing parenthesis of the for loop. As a result, the loop does not execute any statements. After the loop, the code prints "A" and "B" using the cout statement. Therefore, the output of the code is "AB".

    Rate this question:

  • 9. 

    What is the output of the following: #include<iostream> using namespace std; void M(int a ,int b ); int main() { int x=5 , y=10; M(x,y); cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; return 0; } void M(int a ,int b ) {a++; b=b*2; cout<<"a="<<a<<endl; cout<<"b="<<b <<endl; }

    • A.

      A=6 , b=20 , x=5 , y=10

    • B.

      A=7 , b=10 , x=5 , y=10

    • C.

      A=6 , b=20 , x=6 , y=11

    • D.

      A=6 , b=7 , x=5 , y=11

    Correct Answer
    A. A=6 , b=20 , x=5 , y=10
    Explanation
    The code defines a function M that takes two integer parameters and increments the first parameter by 1 and multiplies the second parameter by 2. In the main function, it declares two integer variables x and y with initial values of 5 and 10 respectively. It then calls the function M with x and y as arguments. After the function call, it prints the values of x and y. Since the function M only modifies its local copies of the parameters, the values of x and y remain unchanged. Therefore, the output will be "x=5" and "y=10".

    Rate this question:

  • 10. 

    #include<iostream> using namespace std; void Fun(int a ,int b=20, int c=80 ) { cout<<a<<" "<<b<<" "<<c<<endl;  } void main() { int x=7 , y=6 , z=0; Fun(x,y,z); Fun() return 0;  }

    • A.

      Syntax error

    • B.

      7 6 0

    • C.

      7 7 7

    • D.

      0 8 2

    Correct Answer
    A. Syntax error
    Explanation
    The given code has a syntax error because the function Fun() is called without any arguments in the second function call. The function Fun() is defined with three parameters, but when it is called without any arguments, it causes a syntax error.

    Rate this question:

  • 11. 

    What is the output of this code :  #include<iostream> using namespace std; int x=100; int y=200; int main() { cout<<x<<" "<<y<<endl; int x=3,y=5; cout<<x<<" "<<y<<endl; }

    • A.

      100 200 5 3

    • B.

      100 200 3 5

    • C.

      200 200 3 3

    • D.

      200 100 6 4

    Correct Answer
    B. 100 200 3 5
    Explanation
    The code first declares two global variables x and y with values 100 and 200 respectively. Then, inside the main function, it declares two local variables x and y with values 3 and 5 respectively. When the code prints the values of x and y, it first prints the global variables x and y which are 100 and 200. Then, it prints the local variables x and y which are 3 and 5. Therefore, the output of the code is "100 200" followed by "3 5".

    Rate this question:

  • 12. 

    #include <iostream> using namespace std; int main() {     int a[5]={1,3,2,-2,5};     int sum,p;     int c=1;     sum=a[0];     p=sum;     for (int i=1; i<5; i++)              if (a[i]>p)         {             sum+=a[i];p=a[i]; c++;}     cout<<c<<" "<<sum;      return 0;     } what is the output ?

    • A.

      6 8

    • B.

      3 9

    • C.

      4 5

    • D.

      1 3

    Correct Answer
    B. 3 9
    Explanation
    The program calculates the sum of the elements in the array 'a'. It initializes the variables 'sum' and 'p' to the first element of the array. Then, it iterates through the remaining elements of the array and checks if the current element is greater than 'p'. If it is, it adds the current element to 'sum', updates 'p' to the current element, and increments the counter 'c'. Finally, it prints the value of 'c' followed by the value of 'sum'. In this case, the output will be "3 9".

    Rate this question:

  • 13. 

    #include using namespace std; int F2(int x ) {     return (x+1); } int main() {     cout<

    • A.

      11

    • B.

      12

    • C.

      15

    • D.

      13

    Correct Answer
    D. 13
    Explanation
    The program defines a function F2 that takes an integer x as input and returns x+1. In the main function, the program prints out the numbers 11, 12, 15, and 13. The correct answer is 13 because when the function F2 is called with the input 12, it returns 13.

    Rate this question:

  • 14. 

    #include <iostream> #include <cmath> using namespace std; double s(int d) {     return pow(2,d);     d++; } int main () {     int d=5;     cout<<s(d)<<" "<<d;     return 0; }

    • A.

      33 6

    • B.

      32 6

    • C.

      32 5

    • D.

      23 5

    Correct Answer
    C. 32 5
    Explanation
    The correct answer is 32 5.

    In the given code, the function s() takes an integer argument d and returns 2 raised to the power of d. However, before returning the result, the value of d is incremented by 1.

    In the main function, the variable d is initialized to 5. The s(d) function is called and its result (32) is printed followed by the value of d (5).

    Therefore, the output is 32 5.

    Rate this question:

  • 15. 

    #include <iostream> using namespace std; int func(int x , int y) {     if (x>y)     return x+y;     else     return x-y; } int main () {     for(int i=3;i>=0;i--)     cout<<func(4-i,i)<<" ";      }

    • A.

      -2 0 4 4

    • B.

      -2 4 4 0

    • C.

      -2 0 4 5

    • D.

      -1 6 0 5

    Correct Answer
    A. -2 0 4 4
    Explanation
    The program defines a function called func that takes two integer parameters x and y. Inside the function, it checks if x is greater than y. If it is, it returns the sum of x and y. Otherwise, it returns the difference between x and y.

    In the main function, a for loop is used to iterate from i=3 to i=0. Inside the loop, the func function is called with arguments (4-i) and i. The return value of func is then printed to the console.

    Based on the given code and the values passed to the func function in the loop, the output will be -2 0 4 4.

    Rate this question:

  • 16. 

    #include <iostream> using namespace std; void fun() {     static int x=2;     x++;     cout<<x<<" "; } int main () {     int x=10;     fun();fun();     cout<<x; }

    • A.

      3 5 10

    • B.

      3 4 10

    • C.

      2 3 10

    • D.

      2 10 4

    Correct Answer
    B. 3 4 10
    Explanation
    The function fun() is declared with a static variable x, which means that the value of x will be preserved between function calls. In the main function, the variable x is initialized to 10. When the function fun() is called twice, the value of x is incremented by 1 each time and printed. Therefore, the output will be "3 4" for the first two print statements. Finally, the value of x in the main function is printed, which is still 10. Hence, the output will be "3 4 10".

    Rate this question:

  • 17. 

    #include <iostream> #include <cmath> using namespace std; void operate (int a, int b) {     cout<<(a*sqrt(b));      }      int operate (double a, double b) {     return (a/b); } int main () {     int x=5,y=4;     double n=5.0, m=2.0;     operate(x,y);cout<<" ";     cout<<operate (n,m); }

    • A.

      11 3

    • B.

      10 2

    • C.

      2 10

    • D.

      11 2

    Correct Answer
    B. 10 2
    Explanation
    The first call to the `operate` function passes two integers, `x=5` and `y=4`. Inside the function, it calculates the product of `a` and the square root of `b`, which is equal to `5 * sqrt(4) = 10`. Therefore, the first number in the answer is `10`.

    The second call to the `operate` function passes two doubles, `n=5.0` and `m=2.0`. Inside the function, it calculates the division of `a` by `b`, which is equal to `5.0 / 2.0 = 2.5`. However, the return type of the function is an integer, so the decimal part is truncated and the second number in the answer is `2`.

    Therefore, the correct answer is `10 2`.

    Rate this question:

  • 18. 

    #include <iostream> using namespace std; void main() {     int A[10]={9,2,3,7,-4,5};     double S=0;     int B[3]={2,4,1};     for (int x=0;x<3;x++)     S=S+A[B[x]];     cout<<S; }

    • A.

      1

    • B.

      2

    • C.

      3

    • D.

      4

    Correct Answer
    A. 1
    Explanation
    The code snippet initializes an integer array A with 6 elements and a double variable S with a value of 0. It also declares an integer array B with 3 elements. The for loop iterates over the elements of array B and adds the value at the index B[x] of array A to the variable S. Finally, the value of S is printed. The correct answer is 1 because the for loop only iterates 3 times, and the elements at index 2, 4, and 1 of array A are 3, -4, and 2 respectively, which sum up to 1.

    Rate this question:

  • 19. 

    #include <iostream> using namespace std; int main() {    int x=6;    if (x>0)    switch (x)    {     case 1:        x=x+3;        break;     case 3:         x++;         break;     case 6:         x=x+6;         break;     case 8:         x=x*2;         break;     default:        x--;    }    else    x=x+2;     cout<<x; }

    • A.

      12

    • B.

      24

    • C.

      36

    • D.

      8

    Correct Answer
    A. 12
    Explanation
    The given code snippet checks the value of the variable x. Since x is equal to 6, the switch case statement will execute the code block for case 6. In this case, the value of x is incremented by 6, resulting in x being equal to 12. Finally, the value of x is printed, which is 12.

    Rate this question:

  • 20. 

    #include <iostream> using namespace std; int FunB(int); int FunC(int); int FunA(int y) {     if((2*y%2)==0)     return FunB(y);     else     return FunC(y); } int main() {     for (int x=1;x<=3;x++)     cout<<FunA(x)<<" ";     cout<<endl; } int FunB(int y) {     return y*y; } int FunC(int y) {     return y+y; }

    • A.

      1 3 9

    • B.

      2 4 9

    • C.

      1 4 9

    • D.

      1 5 9

    Correct Answer
    C. 1 4 9
    Explanation
    The program starts by calling the FunA function with the value of 1. Since 2*1%2 is equal to 0, the if condition in FunA is true and it calls the FunB function with the value of 1. FunB returns 1*1, which is 1. This 1 is printed by the cout statement in the main function.

    Next, the program calls the FunA function with the value of 2. Again, 2*2%2 is equal to 0, so FunA calls FunB with the value of 2. FunB returns 2*2, which is 4. This 4 is printed by the cout statement in the main function.

    Finally, the program calls the FunA function with the value of 3. This time, 2*3%2 is not equal to 0, so the else condition in FunA is true and it calls FunC with the value of 3. FunC returns 3+3, which is 6. However, since FunA is called with the value of 3 in the for loop, the return value of FunC is not printed. Instead, the number 9 is printed by the cout statement in the main function.

    Therefore, the output of the program is 1 4 9.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Feb 17, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Apr 29, 2018
    Quiz Created by
    Catherine Halcomb
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.