Ilp C++ Quiz Prelims 1

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 Srikanth_kanthet
S
Srikanth_kanthet
Community Contributor
Quizzes Created: 5 | Total Attempts: 893
Questions: 33 | Attempts: 102

SettingsSettingsSettings
Ilp C++ Quiz Prelims 1 - Quiz

ILP C++ Quiz


Questions and Answers
  • 1. 

    What is the output of the following snippet? int a=0101; int b=101; cout<<a<<” , ”<<b;

    • A.

      0101,101

    • B.

      101,101

    • C.

      Compilation Error

    • D.

      101 0110

    Correct Answer
    C. Compilation Error
    Explanation
    The code snippet will result in a compilation error. This is because the numbers starting with a leading zero (like 0101) are considered as octal numbers in C++. However, the number 101 is not a valid octal number as it contains the digit 8. Therefore, the compiler will throw an error.

    Rate this question:

  • 2. 

    What is the output of the following code ?   int i = 10;   int &r;  r = &i; cout<<i<<":"<<r;

    • A.

      10 10

    • B.

      Compilation Error

    • C.

      10

    • D.

      None of the above

    Correct Answer
    B. Compilation Error
    Explanation
    The given code will result in a compilation error. This is because the statement "int &r; r = &i;" is trying to create a reference variable "r" without initializing it. In C++, reference variables must be initialized when they are declared. Therefore, the code will not compile.

    Rate this question:

  • 3. 

    What is the output of the following snippet ? class Test { }; int main() {  Test t;  cout<<sizeof(t);  return 0; }

    • A.

      0

    • B.

      1

    • C.

      None of the above

    • D.

      Compilation Error

    Correct Answer
    B. 1
    Explanation
    The output of the given snippet is 1. This is because the sizeof operator is used to determine the size in bytes of an object or data type. In this case, sizeof(t) is used to determine the size of the object 't' of type Test. Since the Test class does not have any member variables, its size is 1 byte.

    Rate this question:

  • 4. 

    Which of the following way is the correcy way to set a value 3.14 in a variable pi such that it can not be modified ?

    • A.

      Float pi = 3.14F;

    • B.

      #define pi 3.14F;

    • C.

      Const float pi = 3.14F;

    • D.

      Const float pi; pi = 3.14F

    Correct Answer
    C. Const float pi = 3.14F;
    Explanation
    The correct way to set a value of 3.14 in a variable pi such that it cannot be modified is by using the "const" keyword. By declaring the variable as "const float pi = 3.14F;", it ensures that the value of pi remains constant and cannot be changed throughout the program. This prevents any accidental or intentional modifications to the value of pi.

    Rate this question:

  • 5. 

    What is the output of the following snippet ? unsigned int a = 5; int b=2; int status = (a>b)?1:0; cout<<status;

    • A.

      1

    • B.

      0

    • C.

      01

    • D.

      10

    Correct Answer
    B. 0
    Explanation
    The output of the snippet is 0. This is because the expression "(a > b)" evaluates to false since 5 is not greater than 2. Therefore, the value of "status" is assigned as 0. The "cout" statement then outputs the value of "status", which is 0.

    Rate this question:

  • 6. 

    What is the output of the following snippet ? int *p=10; cout<<*p;

    • A.

      10

    • B.

      Address of '10'

    • C.

      Compilation Error

    • D.

      None of the above

    Correct Answer
    C. Compilation Error
    Explanation
    The given code snippet will result in a compilation error. This is because the variable 'p' is declared as a pointer to an integer, but it is being assigned the value 10 directly instead of the address of an integer variable. This is an invalid assignment and will cause a compilation error.

    Rate this question:

  • 7. 

    What is the output of the following code snippet ? int main() {    int a=1, b=2, c=3,d,e;    d=a,b,c;    e=(a,b,c);    cout<<d<<e;

    • A.

      1,1

    • B.

      1,3

    • C.

      3,3

    • D.

      3,1

    Correct Answer
    B. 1,3
    Explanation
    The output of the code snippet is 1,3. In the line "d=a,b,c;", the comma operator is used. The comma operator evaluates all the expressions and returns the value of the last expression. So, d is assigned the value of c, which is 3. In the line "e=(a,b,c);", the comma operator is also used. However, in this case, the comma operator evaluates all the expressions but returns the value of the second expression. So, e is assigned the value of b, which is 1. Finally, the values of d and e are printed, resulting in 1,3.

    Rate this question:

  • 8. 

    What is the output of the following snippet ? int a=5.6; float b=5; cout<<a<<”\t”<<b;

    • A.

      5.6,5

    • B.

      5,5.0

    • C.

      5,5 and warning

    • D.

      Error

    Correct Answer
    C. 5,5 and warning
    Explanation
    The output of the snippet is "5, 5 and warning". The variable 'a' is declared as an integer but assigned a floating-point value, which results in a warning. The value of 'a' is truncated to 5. The variable 'b' is declared as a float and assigned an integer value, which is valid. The output statement prints the values of 'a' and 'b' separated by a tab character.

    Rate this question:

  • 9. 

    What is the output of the following snippet ? double a=10.5; double b=4.0 cout<<a%b<<” “<<a*b; cout<<” “<<a+b<<” “<<a-b; 

    • A.

      2.5 5.0

    • B.

      Compilation Error

    • C.

      2

    • D.

      None of the Above

    Correct Answer
    B. Compilation Error
    Explanation
    The given code snippet will result in a compilation error. The reason for this is that the modulus operator (%) cannot be used with double data types. The modulus operator can only be used with integer data types. Therefore, the statement "cout

    Rate this question:

  • 10. 

    What is the output of the following snippet ? char* const p="abc";  cout<<p; p=“x”; cout<<p;  

    • A.

      Abc,x

    • B.

      Abc,xbc

    • C.

      Segmentation Fault

    • D.

      Compilation Error

    Correct Answer
    D. Compilation Error
    Explanation
    The given code snippet will result in a compilation error. This is because the variable "p" is declared as a constant pointer to a character, meaning that the pointer itself cannot be changed to point to a different memory location. Therefore, the assignment "p = "x";" will give a compilation error.

    Rate this question:

  • 11. 

    What is the output of the following snippet ? int main() {   int a=10;   {      a=20;     {        a=30;    cout<<a;      }            cout<<a;           }       cout<<a; }

    • A.

      30,20,10

    • B.

      30,30,30

    • C.

      10,20,30

    • D.

      Compilation Error

    Correct Answer
    B. 30,30,30
    Explanation
    The output of the snippet is 30, 30, 30.

    This is because the variable 'a' is assigned the value of 10 initially. Then, within the innermost block, 'a' is reassigned the value of 30. This value is then printed within that block. After that, the value of 'a' is still 30 when it is printed again within the outer block. Finally, when 'a' is printed outside of any block, it is still 30. Therefore, the output is 30, 30, 30.

    Rate this question:

  • 12. 

    What is the output of the following snippet ? cout<<sizeof(NULL)<<" "<<sizeof("");

    • A.

      0,0

    • B.

      8,8

    • C.

      8,1

    • D.

      4,0

    • E.

      4,1

    • F.

      4,4

    Correct Answer
    C. 8,1
    Explanation
    The output of the snippet is "8,1". The sizeof(NULL) returns the size of a pointer, which is typically 8 bytes on a 64-bit system. The sizeof("") returns the size of an empty string, which is 1 byte (for the null terminator character).

    Rate this question:

  • 13. 

    What is the output of the following snippet ? int a(2); int b=2; cout<<a<<”\t”<<b;

    • A.

      2,2

    • B.

      Compilation Error

    • C.

      Garbage Value, 2

    • D.

      2,Garbage Value

    Correct Answer
    A. 2,2
    Explanation
    The output of the given snippet is "2 2". The variable "a" is initialized with the value 2, and the variable "b" is also assigned the value 2. The "cout" statement prints the values of "a" and "b" separated by a tab character.

    Rate this question:

  • 14. 

    What is the output of the following snippet ? int main() {    cout<<a;    return 0; } int a=10;

    • A.

      10

    • B.

      0

    • C.

      Runtime Error

    • D.

      Compilation Error

    Correct Answer
    D. Compilation Error
    Explanation
    The code snippet will result in a compilation error. This is because the variable "a" is declared and initialized after the cout statement. In C++, variables must be declared before they are used. Therefore, the compiler will throw an error indicating that "a" is not declared in the scope of the cout statement.

    Rate this question:

  • 15. 

    What is the Output of the following code ? int a=1,b=2; a^=b^=a^=b; cout<<a<<" "<<b;

    • A.

      1,2

    • B.

      1,1

    • C.

      2,1

    • D.

      2,2

    Correct Answer
    C. 2,1
    Explanation
    The code uses the XOR operator (^) to swap the values of variables a and b.

    First, a^=b^=a^=b is evaluated as a^=b^=1^=b. Since b is initially 2, b^=1^=2 is evaluated as b^=3. This means that b is updated to 1.

    Next, a^=1^=2 is evaluated as a^=3. Since a is initially 1, this means that a is updated to 2.

    Therefore, the final values of a and b are 2 and 1 respectively.

    Rate this question:

  • 16. 

    What is the output of the following snippet ? int a = 5+NULL; cout<<a;

    • A.

      5

    • B.

      NULL

    • C.

      Error

    • D.

      None Of the Above

    Correct Answer
    A. 5
    Explanation
    The output of the given snippet is 5. When adding a NULL value to an integer, it is treated as 0. Therefore, 5 + NULL is equivalent to 5 + 0, resulting in 5.

    Rate this question:

  • 17. 

    What is the output of the following snippet ? float a=3.5;   a++; char c='A'; c++; int i=-2; i++; cout<<a<<"\t"<<c<<"\t"<<i;

    • A.

      3.6 B -3

    • B.

      4.6 B -1

    • C.

      4.5 B -1

    • D.

      Compilation Error

    Correct Answer
    C. 4.5 B -1
    Explanation
    The output of the given snippet is "4.5 B -1".


    - The variable "a" is initialized with the value 3.5 and then incremented by 1 using the post-increment operator. So, "a" becomes 4.5.
    - The variable "c" is initialized with the character 'A' and then incremented by 1 using the post-increment operator. The ASCII value of 'A' is 65, so after incrementing, "c" becomes 'B'.
    - The variable "i" is initialized with the value -2 and then incremented by 1 using the post-increment operator. So, "i" becomes -1.
    - Finally, the values of "a", "c", and "i" are printed using the cout statement.

    Rate this question:

  • 18. 

    What is the output of the following snippet ? int main() {   int a=1; cout<<a+++++a; }

    • A.

      3

    • B.

      4

    • C.

      Compilation Error

    • D.

      None of the above

    Correct Answer
    C. Compilation Error
    Explanation
    The given code snippet will result in a compilation error. This is because the expression "a+++++a" is ambiguous and not valid in C++. The compiler cannot determine the correct meaning of this expression, leading to a compilation error.

    Rate this question:

  • 19. 

    What is the output of the following snippet ? #define SQUARE(X) (X*X) int main() {  cout<<SQUARE(5);  cout<<SQUARE(2+3); }

    • A.

      25,25

    • B.

      25,13

    • C.

      Compilation Error

    • D.

      None Of the above

    Correct Answer
    B. 25,13
    Explanation
    The output of the snippet is 25, 13.

    The macro SQUARE(X) is defined as (X*X), so when SQUARE(5) is called, it is replaced with (5*5), which evaluates to 25.

    Similarly, when SQUARE(2+3) is called, it is replaced with (2+3*2+3), which evaluates to 2+9, resulting in 11.

    Therefore, the output is 25, 13.

    Rate this question:

  • 20. 

    What is the output of the following snippet? int a=10; cout<<a++*a++<<”\t”; cout<<a++*++a<<”\t”;

    • A.

      110,156

    • B.

      100,169

    • C.

      256,225

    • D.

      None Of the above

    Correct Answer
    B. 100,169
    Explanation
    The output of the first statement is 100 because the post-increment operator increases the value of 'a' to 11 and then multiplies it by the original value of 'a' which is 10. The output of the second statement is 169 because the first post-increment operator increases 'a' to 11, then the pre-increment operator increases it to 12, and then it is multiplied by the original value of 'a' which is 12.

    Rate this question:

  • 21. 

    What is the output of the following snippet ? cout<<2/9<<"\t"; cout<<2.0/9<<"\t"; cout<<2/9.0<<"\t"; cout<<2.0/9.0<<"\t";

    • A.

      0.2 0 0 0.2

    • B.

      0 0 0 0.2

    • C.

      0 0.2 0.2 0.2

    • D.

      0 0.2 0 0.2

    Correct Answer
    C. 0 0.2 0.2 0.2
    Explanation
    The first cout statement outputs the result of integer division 2/9, which is 0. The second cout statement outputs the result of floating-point division 2.0/9, which is approximately 0.2222. The third cout statement outputs the result of floating-point division 2/9.0, which is approximately 0.2222. The fourth cout statement outputs the result of floating-point division 2.0/9.0, which is approximately 0.2222. Therefore, the correct answer is 0 0.2 0.2 0.2.

    Rate this question:

  • 22. 

    What is the output of the following snippet ? short int a=1; a=a<<15; cout<<a;

    • A.

      -32768

    • B.

      32767

    • C.

      30

    • D.

      None of the Above

    Correct Answer
    A. -32768
    Explanation
    The output of the given snippet is -32768. In this code, the variable 'a' is initially assigned the value 1. Then, the bitwise left shift operator '

    Rate this question:

  • 23. 

    What is the output of the following snippet  ?   int array[10] = {2,1,4,5};   int &r = array[2];   cout<<*r;

    • A.

      4

    • B.

      Compilation Error

    • C.

      1

    • D.

      None of the above

    Correct Answer
    B. Compilation Error
    Explanation
    The code snippet declares an array of integers with a size of 10 and initializes the first four elements with the values 2, 1, 4, and 5. It then creates a reference variable 'r' that refers to the third element of the array. Finally, it prints the value pointed to by 'r', which is 4. Therefore, the correct answer is 4. There is no compilation error in this code.

    Rate this question:

  • 24. 

    What is the output of the following code snippet ? int a=1,b=2; int x=!!a; int y=!a&&b; cout<<x<<”\t”<<y;

    • A.

      1 1

    • B.

      1 0

    • C.

      0 0

    • D.

      True false

    Correct Answer
    B. 1 0
    Explanation
    The code snippet initializes two variables, "a" with the value 1 and "b" with the value 2. Then, it assigns the value of the logical NOT operator applied twice to "a" to the variable "x". This means that "x" will be 1, as the logical NOT operator returns true (1) if the operand is false (0), and false (0) if the operand is true (non-zero). Next, it assigns the value of the logical AND operator applied to the logical NOT of "a" and "b" to the variable "y". Since "a" is true (non-zero), the logical NOT of "a" is false (0), and the logical AND operator returns false (0) if any of its operands are false (0). Therefore, "y" will be 0. Finally, the code prints the values of "x" and "y" separated by a tab character. So, the output will be "1 0".

    Rate this question:

  • 25. 

    What is the output of the following code snippet ? int a=1,b=1; int x=(a<2)&&(b=100); int y=(a==b)&&(a!=b); cout<<x<<”\t”<<y<<”\t”<<a<<”\t”<<b;

    • A.

      1 1 1 0

    • B.

      0 100 0 0

    • C.

      1 0 1 1

    • D.

      1 0 1 100

    Correct Answer
    D. 1 0 1 100
    Explanation
    The code snippet initializes two variables, a and b, with the value 1.

    In the line "int x=(a

    Rate this question:

  • 26. 

    What is the output of the following code snippet ? int a=-10; cout<<!(~a);

    • A.

      0

    • B.

      1

    • C.

      -10

    • D.

      9

    Correct Answer
    A. 0
    Explanation
    The code snippet initializes the variable 'a' with the value -10. The bitwise complement operator '~' inverts the bits of 'a', resulting in 9. The logical negation operator '!' is then applied to 9, which converts any non-zero value to 0. Therefore, the output of the code is 0.

    Rate this question:

  • 27. 

    What is the output of the following code snippet ? int a=-10, b=-22; cout<<a++<<a++<<b++<<b++;

    • A.

      -10 -9 -22 -21

    • B.

      -9 -10 -21 -22

    • C.

      -10 -10 -22 -22

    • D.

      -9 -9 -21 -21

    Correct Answer
    B. -9 -10 -21 -22
    Explanation
    The code snippet uses the post-increment operator (a++) and the stream insertion operator (

    Rate this question:

  • 28. 

    What is the output of the following code snippet ? int a=10; cout<<++a*a++<<”\t”<<++a*++a;

    • A.

      169 144

    • B.

      182 121

    • C.

      121 182

    • D.

      None of the above

    Correct Answer
    A. 169 144
    Explanation
    The code snippet uses the pre-increment and post-increment operators on the variable 'a'. In the first part of the code, '++a' increments 'a' to 11 and then 'a++' returns the value 11 but increments 'a' to 12. So, the expression '++a*a++' becomes 11 * 11 which is 121. In the second part of the code, '++a' increments 'a' to 13 and then '++a' increments 'a' to 14. So, the expression '++a*++a' becomes 13 * 14 which is 182. Therefore, the output of the code snippet is 169 144.

    Rate this question:

  • 29. 

    What is the output of the following snippet ? if(cout<<"hello") {  cout<<"hi"; }

    • A.

      Hello

    • B.

      Hellohi

    • C.

      Hello hi

    • D.

      Hi

    Correct Answer
    B. Hellohi
    Explanation
    The output of the snippet is "hellohi". This is because the first "cout" statement prints "hello", and since it is within the "if" condition, the second "cout" statement is also executed and prints "hi". Therefore, the output is the combination of both strings, "hello" and "hi".

    Rate this question:

  • 30. 

    What is the output of the following code snippet ? int a; for(a=0;a++;a++);   cout<<a;

    • A.

      1

    • B.

      0

    • C.

      2

    • D.

      Compilation Error

    Correct Answer
    A. 1
    Explanation
    The code snippet initializes the variable 'a' as 0. Then, it enters a for loop where the condition 'a++' is checked. However, since the increment operation is placed after the semicolon, the condition will always be false and the loop will not execute. Therefore, the value of 'a' remains 0. Finally, the value of 'a' is printed, which is 0.

    Rate this question:

  • 31. 

    What is the output of the following code snippet ? if(int i=1)    i++; else    i--; cout<<i;

    • A.

      2

    • B.

      1

    • C.

      Compilation Error

    • D.

      0

    Correct Answer
    C. Compilation Error
    Explanation
    The code snippet will result in a compilation error. This is because the if statement requires a boolean expression inside the parentheses, but instead, the code snippet has an initialization statement for the variable i.

    Rate this question:

  • 32. 

    What is the output of the following code snippet ? class ABC {   int i;   public:       ABC(int i = 0)       {          this.i = i;       }      void display()      {            cout<<i;      } } int main() {    ABC obj(10);   obj.display(); }

    • A.

      Compilation Error

    • B.

      0

    • C.

      10

    • D.

      No Output

    Correct Answer
    A. Compilation Error
    Explanation
    The code snippet will result in a compilation error. This is because the line "this.i = i;" is not a valid syntax in C++. The correct syntax to assign a value to a member variable in a constructor is "this->i = i;". Since the code has a compilation error, it will not produce any output.

    Rate this question:

  • 33. 

    What is the output of the following code snippet ? int i; for(i=1;i<=10;i++,cout<<i<<endl);

    • A.

      1..11

    • B.

      2..11

    • C.

      1,10

    • D.

      Compilation Error

    Correct Answer
    B. 2..11
    Explanation
    The code snippet is a for loop that initializes an integer variable i to 1, checks if i is less than or equal to 10, increments i by 1, and prints the value of i followed by a new line. The loop continues until i is no longer less than or equal to 10. Therefore, the output will be a sequence of numbers from 2 to 11, each on a new line.

    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
  • Sep 04, 2012
    Quiz Created by
    Srikanth_kanthet
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.