C++ Toughest Exam Questions! Trivia

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Catherine Halcomb
Catherine Halcomb
Community Contributor
Quizzes Created: 1443 | Total Attempts: 6,714,231
| Attempts: 242 | Questions: 20
Please wait...
Question 1 / 20
0 %
0/100
Score 0/100
1. 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; }

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".

Submit
Please wait...
About This Quiz
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... see moreout just how much you understand about it. How about you give it a shot and see how well you will do. see less

Personalize your quiz and earn a certificate with your name on it!
2. 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; }

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".

Submit
3. #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;  }

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.

Submit
4. Formal parameters are necessary for a void function.

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.

Submit
5. 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; }  

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".

Submit
6. #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; }

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.

Submit
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; }

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.

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

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.

Submit
9. #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); }

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`.

Submit
10. #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)<<" ";      }

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.

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

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".

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

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.

Submit
13. #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 ?

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".

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

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.

Submit
15. #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; }

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.

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

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.

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

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.

Submit
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; }

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.

Submit
19. #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; }

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.

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

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.

Submit
View My Results

Quiz Review Timeline (Updated): Feb 17, 2023 +

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
Cancel
  • All
    All (20)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What is the output of this code : ...
What is the output of the following:...
#include<iostream> ...
Formal parameters are necessary for a void function.
What is the output of the following code: ...
#include <iostream> ...
What is the output of the following code:...
Function pow(x,y) returns a value of type int.
#include <iostream> ...
#include <iostream> ...
#include <iostream> ...
A value returning function is used in an assignment or in an input...
#include <iostream>...
Value parameter: a formal parameter that receives the location of the...
#include <iostream> ...
It's necessary to specify the variable name in the parameter list.
Formal parameter: variable or expression listed in a call to a...
#include <iostream> ...
#include <iostream> ...
#include ...
Alert!

Advertisement