Trivia: Test On Output Of C Programs! Quiz

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 Himasri
H
Himasri
Community Contributor
Quizzes Created: 5 | Total Attempts: 2,574
Questions: 10 | Attempts: 335

SettingsSettingsSettings
Trivia: Test On Output Of C Programs! Quiz - Quiz

Are you a computer programmer looking for a way to refresh what you know when it comes to the output of C programs? If you said yes, then you are in luck as the quiz is prepared to help you do just that. Give it a try and see if beyond might need to spend more hours studying for your finals.


Questions and Answers
  • 1. 

    What will be output if you will compile and execute the following c code? #include<stdio.h> int main(){   register int i,x;   scanf("%d",&i);   x=++i + ++i + ++i;   printf("%d",x);   return 0; }

    • A.

      17

    • B.

      18

    • C.

      21

    • D.

      22

    • E.

      Compiler Error

    Correct Answer
    E. Compiler Error
    Explanation
    The code will result in a compiler error. The reason for this is the use of the "register" keyword before the variable declaration. In modern C programming, the "register" keyword is considered obsolete and is no longer necessary. Some compilers may still support it, but in this case, it causes a conflict with the use of the "++" operator multiple times on the same variable in the expression "++i + ++i + ++i". This results in undefined behavior and a compiler error.

    Rate this question:

  • 2. 

    What will be output if you will compile and execute the following c code? #include<stdio.h> int main(){   int a=5;   int b=10;   {     int a=2;     a++;     b++;   }   printf("%d %d",a,b);   return 0; }

    • A.

      5 10

    • B.

      6 11

    • C.

      5 11

    • D.

      6 10

    • E.

      Compiler error

    Correct Answer
    C. 5 11
    Explanation
    The code declares two variables, a and b, and initializes them with the values 5 and 10 respectively. Then, inside a block of code, a new variable a is declared and initialized with the value 2. The value of this inner a is then incremented by 1, but it does not affect the value of the outer a. Similarly, the value of b is incremented by 1. Finally, the values of the outer a and b are printed, which are 5 and 11 respectively.

    Rate this question:

  • 3. 

    What will be output if you will compile and execute the following c code? #include<stdio.h> int main(){   float f=3.4e39;   printf("%f",f);   return 0; }

    • A.

      3.4e39

    • B.

      3.40000…

    • C.

      +INF

    • D.

      Compiler error

    • E.

      Run time error

    Correct Answer
    C. +INF
    Explanation
    The code declares a variable 'f' of type float and assigns it the value 3.4e39, which is a very large number. The printf function is then used to print the value of 'f'. Since the value of 'f' exceeds the maximum representable value for a float, it will be represented as positive infinity (+INF) when printed.

    Rate this question:

  • 4. 

    What will be output if you will compile and execute the following c code? #include<stdio.h> int main(){   enum color{     RED,GREEN=-20,BLUE,YELLOW   };   enum color x;   x=YELLOW;   printf("%d",x);   return 0; }

    • A.

      -22

    • B.

      -18

    • C.

      1

    • D.

      Compiler error

    • E.

      None of the above

    Correct Answer
    B. -18
    Explanation
    The code defines an enumeration called "color" with four possible values: RED, GREEN, BLUE, and YELLOW. The values are assigned implicitly, with RED being 0, GREEN being -20, BLUE being -19, and YELLOW being -18. The variable "x" is declared as an enum color and is assigned the value YELLOW. The printf statement then prints the value of "x", which is -18.

    Rate this question:

  • 5. 

    What will be output if you will compile and execute the following c code? #include<stdio.h> int main(){   asm{     mov bx,8;     mov cx,10     add bx,cx;   }   printf("%d",_BX);   return 0; }

    • A.

      18

    • B.

      8

    • C.

      0

    • D.

      Compiler error

    • E.

      None of above

    Correct Answer
    A. 18
    Explanation
    The given C code uses inline assembly to perform addition of the values stored in the registers bx and cx. The result, 18, is then printed using the printf function. Therefore, the output of the code will be 18.

    Rate this question:

  • 6. 

    What will be output if you will compile and execute the following c code? #include<stdio.h> int main(){   enum xxx{      a,b,c=32767,d,e   };   printf("%d",b);   return 0; }

    • A.

      0

    • B.

      1

    • C.

      32766

    • D.

      Compiler error

    • E.

      None of above

    Correct Answer
    D. Compiler error
    Explanation
    The code will not compile because the enum value "b" is not assigned a value.

    Rate this question:

  • 7. 

    What will be output if you will compile and execute the following c code? #include<stdio.h> int main(){   char *url="c:\tc\bin\rw.c";   printf("%s",url);   return 0; }

    • A.

      C:\tc\bin\rw.c

    • B.

      C:/tc/bin/rw.c

    • C.

      C: c inw.c

    • D.

      C:cinw.c

    • E.

      W.c in

    Correct Answer
    E. W.c in
    Explanation
    The given code declares a character pointer variable named "url" and assigns it the value "c:\tc\bin\rw.c". The printf statement then prints the value of "url", which is "c:\tc\bin\rw.c". Therefore, the output of the code will be "c:\tc\bin\rw.c".

    Rate this question:

  • 8. 

    What will be output if you will compile and execute the following c code? #include<stdio.h> int main(){   goto abc;   printf("main");   return 0; } void dispaly(){   abc:     printf("display"); }

    • A.

      Main

    • B.

      Display

    • C.

      Maindisplay

    • D.

      Displaymain

    • E.

      Compiler error

    Correct Answer
    E. Compiler error
    Explanation
    The code will result in a compiler error because the label "abc" is defined within the function "dispaly()" but it is being used outside of that function in the "main()" function. Labels can only be used within the function they are defined in.

    Rate this question:

  • 9. 

    What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run?

    • A.

      10

    • B.

      9

    • C.

      0

    • D.

      1

    Correct Answer
    A. 10
    Explanation
    This code uses a for loop to increment the value of x. Here's how it works:
    Initialization: x = 0; The variable x is initialized to 0.
    Condition: x < 10; The loop continues as long as x is less than 10.
    Increment: x++ After each iteration of the loop, x is incremented by 1.
    The loop runs 10 times, with x taking on the values 0, 1, 2, 3,...9. After the 10th iteration, x becomes 10, which fails the condition x < 10, causing the loop to terminate. Therefore, the final value of x is 10.

    Rate this question:

  • 10. 

    What will be output if you will compile and execute the following c code? #include<stdio.h> void call(int,int,int); int main(){   int a=10;   call(a,a++,++a);   return 0; } void call(int x,int y,int z){   printf("%d %d %d",x,y,z); }  

    • A.

      10 10 12

    • B.

      12 11 12

    • C.

      12 12 12

    • D.

      10 11 12

    • E.

      Compiler Error

    Correct Answer
    B. 12 11 12
    Explanation
    This code snippet demonstrates the behavior of post-increment (a++) and pre-increment (++a) operators in C, along with the order of evaluation of function arguments.
    Pre-increment (++a): The value of a is incremented before it's used in the expression.
    Post-increment (a++): The value of a is incremented after it's used in the expression.
    Order of evaluation:
    While the order of evaluation of function arguments in C is unspecified, in this particular case, it seems the compiler evaluates them from right to left. Here's the breakdown:
    ++a: a is incremented to 11, then to 12. This 12 is passed as the third argument (z).
    a++: The current value of a (12) is passed as the second argument (y). Then, a is incremented to 13 (but this value is unused).
    a: The current value of a (12) is passed as the first argument (x).
    Therefore, the call function receives the values 12, 11, and 12 for x, y, and z respectively, resulting in the output "12 11 12".

    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
  • Sep 30, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Mar 06, 2012
    Quiz Created by
    Himasri
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.