C Programming Practice - Test 3

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 Nchaudhuri31
N
Nchaudhuri31
Community Contributor
Quizzes Created: 19 | Total Attempts: 9,428
Questions: 20 | Attempts: 330

SettingsSettingsSettings
C Programming Practice - Test 3 - Quiz

. Dear Learner,
This Assessment consists of questions to test your understanding on - C Programming Basics
You may attempt the same and clarify doubts in WP Group. / With your Faculty Member
Regards,
Team SHIVAM


Questions and Answers
  • 1. 

    What will be output if you will compile and execute the following c code? void main() { float a=5.2; if(a==5.2) printf("Equal"); else if(a

    • A.

      Less than

    • B.

      Equal

    • C.

      Greter than

    • D.

      Compilation error

    Correct Answer
    A. Less than
    Explanation
    The code will output "Less than" because the value of "a" is not exactly equal to 5.2 due to floating point precision.

    Rate this question:

  • 2. 

    What will be output if you will compile and execute the following c code? void main() { int a=2; if(a==2) { a=~a+2

    • A.

      -3

    • B.

      -2

    • C.

      1

    • D.

      Compilation error

    Correct Answer
    D. Compilation error
    Explanation
    The code will result in a compilation error. This is because the line "a=~a+2" is not a valid expression in C. The "~" operator is the bitwise complement operator, which inverts the bits of a value. However, it cannot be used with the addition operator "+". Therefore, the code will fail to compile.

    Rate this question:

  • 3. 

    What will be output if you will compile and execute the following c code? void main() { int i=10; static int x=i; if(x==i) printf("Equal"); else if(x>i) printf("Greater than"); else printf("Less than"); }

    • A.

      Less than

    • B.

      Equal

    • C.

      Greter than

    • D.

      Compilation error

    Correct Answer
    B. Equal
    Explanation
    The code declares a variable i with a value of 10 and a static variable x with the value of i. The if statement checks if x is equal to i, and if so, it prints "Equal". Since the value of x is assigned as i, which is 10, the condition is true and "Equal" is printed as the output.

    Rate this question:

  • 4. 

    What will be output if you will compile and execute the following c code? main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }

    • A.

      I love U

    • B.

      I hate U

    • C.

      Compiler Error

    • D.

      None of These

    Correct Answer
    B. I hate U
    Explanation
    The output will be "I hate U" because the float data type has less precision than the double data type. Although both "me" and "you" are assigned the same value of 1.1, the float data type may not be able to accurately represent the number and may have a slight difference in value compared to the double data type. Therefore, the condition "me==you" will evaluate to false and the program will execute the else statement, printing "I hate U".

    Rate this question:

  • 5. 

    What will be output if you will compile and execute the following c code? main() { static int var = 5; printf("%d ",var--); if(var) main(); }

    • A.

      4 3 2 1

    • B.

      Compilation Error

    • C.

      5 4 3 2 1

    • D.

      5 4 3 2

    Correct Answer
    C. 5 4 3 2 1
    Explanation
    The code defines a static variable "var" with an initial value of 5. It then prints the value of "var" and decrements it by 1. After that, it checks if "var" is still non-zero and if so, it calls the "main" function recursively. This process continues until "var" becomes 0. Therefore, the output will be 5 4 3 2 1.

    Rate this question:

  • 6. 

    What will be output if you will compile and execute the following c code? main() { int i=1; while (i2) goto here; i++; }} fun() { here: printf("PP"); }

    • A.

      1

    • B.

      Pp

    • C.

      Compiler Error

    • D.

      None of These

    Correct Answer
    C. Compiler Error
    Explanation
    The code will result in a compiler error because the function "fun" is defined after it is being used in the "goto" statement. In C, functions need to be declared or defined before they are used. Since the function "fun" is not declared or defined before the "goto" statement, the compiler will throw an error.

    Rate this question:

  • 7. 

    What will be output if you will compile and execute the following c code? #define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }

    • A.

      1

    • B.

      Linter Error

    • C.

      Compiler Error

    Correct Answer
    A. 1
    Explanation
    The output of this code will be "TRUE". In C, any non-zero value is considered as true and zero is considered as false. In this code, the condition `NULL` is equivalent to `0`, which is false. The condition `FALSE` is equivalent to `-1`, which is true. Therefore, the second condition is true and the code will output "TRUE".

    Rate this question:

  • 8. 

    What will be output if you will compile and execute the following c code? #include int x; void main() { if (x) printf("hi"); else printf("how are u"); }

    • A.

      Hi

    • B.

      How are you

    • C.

      Compile Time Error

    • D.

      None of These

    Correct Answer
    B. How are you
    Explanation
    The output of the given C code will be "how are you". This is because the variable "x" is not initialized, so its value is undefined. In C, any non-zero value is considered as true and zero is considered as false. Since the value of "x" is not explicitly set, it will have an indeterminate value which is treated as false. Therefore, the code will execute the "else" block and print "how are you".

    Rate this question:

  • 9. 

    Predict the output/error. main() { int a = 4, b = 4; switch(a) { case 3 : printf(“\n a is 3”); break; case b : printf(“\n a is 4”); // case b : is not possible break; // because constant is needed default : printf(“\n a is not 3 or 4”); } return 0; }

    • A.

      Compilation error

    • B.

      A is 4

    • C.

      A is not 3 or 4

    • D.

      A id 3

    Correct Answer
    A. Compilation error
    Explanation
    The given code will result in a compilation error. This is because the switch statement in C requires constant expressions as case labels. In this code, the case label "b" is not a constant expression as it is a variable. Therefore, the code will not compile.

    Rate this question:

  • 10. 

    The output of the code below is(when 1 is entered) #include void main() { double ch; printf("enter a value btw 1 to 2:"); scanf("%lf", &ch); switch (ch) // ch is declared as duble, but switch will accept { // always an integral value case 1: printf("1"); break; case 2: printf("2"); break; } }

    • A.

      Compile time error

    • B.

      1

    • C.

      2

    • D.

      No output

    Correct Answer
    A. Compile time error
    Explanation
    The code will result in a compile time error because the switch statement requires an integral value, but the variable "ch" is declared as a double.

    Rate this question:

  • 11. 

    What will be output if you will compile and execute the following c code? void main() { int a=2; if(a==2) { a=~a+2

    • A.

      It will print nothing

    • B.

      -3

    • C.

      -2

    • D.

      1

    Correct Answer
    A. It will print nothing
    Explanation
    The code snippet declares a variable 'a' and assigns it the value 2. It then checks if 'a' is equal to 2. Since 'a' is indeed equal to 2, the if statement is true. Inside the if statement, the bitwise complement operator (~) is applied to 'a' and then 2 is added to the result. The bitwise complement of 2 is -3 in two's complement representation. Therefore, the expression evaluates to -3 + 2, which is -1. However, there is no code to print or output this value, so the program will not display anything.

    Rate this question:

  • 12. 

    The output of the following code will be main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); }

    • A.

      2000 is a leap year

    • B.

      2000 is not a leap year

    • C.

      Compiler error

    Correct Answer
    A. 2000 is a leap year
    Explanation
    The output of the code will be "2000 is a leap year" because the condition in the if statement is true. The year 2000 is divisible by 4 and 100, so the first part of the condition is true. Since the first part is true, the overall condition is true and the code will execute the printf statement that says "2000 is a leap year".

    Rate this question:

  • 13. 

    The output of the following code will be main() { char c=' ', x, convert(z); getc(c); if((c>='a') && (c

    • A.

      Garbage value

    • B.

      32

    • C.

      Compiler error

    • D.

      ‘z’

    Correct Answer
    C. Compiler error
    Explanation
    The code snippet provided has several syntax errors. The function `getc()` is being used without any arguments, which is incorrect. Additionally, the function `convert()` is being called with an undefined variable `z`. These errors will result in a compiler error.

    Rate this question:

  • 14. 

    The output of the following code will be main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); }

    • A.

      1 0

    • B.

      0..0

    • C.

      1..1

    • D.

      0..1

    Correct Answer
    B. 0..0
    Explanation
    The output of the code will be "0..0".

    In the code, the variable i is initialized to 0 and j is not declared properly. Therefore, it will cause a compilation error. However, assuming that it is a typo and j is intended to be declared as int j = 0, the if condition i && j++ will evaluate to false since i is 0. Therefore, the printf statement inside the if condition will not be executed. The final printf statement will print the values of i and j, which are both 0. Hence, the output will be "0..0".

    Rate this question:

  • 15. 

    The output of the following code will be main() { int a= 0;int b = 20;char x =1;char y =10; if(a,b,x,y) printf("hello"); }

    • A.

      Hello

    • B.

      Compiler error

    • C.

      20

    • D.

      10

    Correct Answer
    A. Hello
    Explanation
    The code will output "hello". The if statement in the code is using the comma operator, which evaluates all the expressions but only returns the result of the last expression. In this case, the last expression is "y", which has a value of 10. Since the value of "y" is not 0, the if statement evaluates to true and the printf statement is executed, resulting in the output "hello".

    Rate this question:

  • 16. 

    The output of the following code will be void main() { static int i=5; if(--i){ main(); printf("%d ",i); }

    • A.

      5

    • B.

      -4 -4 -4

    • C.

      0 0 0 0

    • D.

      3 3 3

    Correct Answer
    C. 0 0 0 0
    Explanation
    The output of the code will be "0 0 0 0".

    The code uses a recursive function main() that is called multiple times. The static variable i is initially set to 5. In each recursive call, the value of i is decremented by 1 before the if condition is checked. Since i is initially 5, the condition (--i) will be true for the first 5 recursive calls.

    Inside each recursive call, the main() function is called again, resulting in more recursive calls.

    Once i becomes 0, the if condition evaluates to false and the recursive calls stop.

    After that, each recursive call prints the value of i, which will be 0 in all cases.

    Therefore, the output will be "0 0 0 0".

    Rate this question:

  • 17. 

    The output of the following code will be void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); }

    • A.

      OK here

    • B.

      Compiler error

    • C.

      Forget

    • D.

      Print nothing

    Correct Answer
    A. OK here
    Explanation
    The output of the code will be "OK here". This is because the if statement checks the result of the printf function, which prints the string stored in the variable "a". Since the string is empty ("\0"), the printf function will return 0, which is considered false in the if statement. Therefore, the else block will not be executed and the code will print "OK here".

    Rate this question:

  • 18. 

    The output of the following code will be void main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; }

    • A.

      Garbage value

    • B.

      Compiler error

    • C.

      % d

    • D.

      Print nothing

    Correct Answer
    A. Garbage value
    Explanation
    The output of the code will be a garbage value. This is because the inner printf statement will print the number of characters printed, which is the return value of the outer printf statement. Since the outer printf statement does not have any arguments, it will return 0, which will be printed by the inner printf statement. However, the value of 0 does not have any significance in terms of printing a meaningful value, hence it is considered as a garbage value.

    Rate this question:

  • 19. 

    The output of the following code will be #include main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); }

    • A.

      2 2

    • B.

      50 50

    • C.

      0 0

    • D.

      Garbage 0

    Correct Answer
    D. Garbage 0
    Explanation
    The output of the code will be "Garbage 0". This is because the variable "x" is not initialized, so its value is undefined. When the expression "x=y%2" is evaluated, the value of "y" is 2 and the remainder of 2 divided by 2 is 0. Therefore, "x" will be assigned the value 0. Since "z" is not assigned a value before the printf statement, its value will be garbage or undefined. The final output will be "Garbage 0".

    Rate this question:

  • 20. 

    The output of the following code will be main() { int i=10,j=20; j = i, j?(i,j)?i:j:j; printf("%d %d",i,j); }

    • A.

      20 20

    • B.

      10 10

    • C.

      0 0

    • D.

      Garbage values

    Correct Answer
    B. 10 10
    Explanation
    The output of the code will be "10 10". This is because the expression "j = i, j?(i,j)?i:j:j" can be broken down as follows:
    - First, the value of i (which is 10) is assigned to j.
    - Then, the ternary operator is evaluated. Since j is not equal to 0 (which is considered as false), the expression "(i,j)?i:j" is evaluated.
    - In this expression, the comma operator is used. The comma operator evaluates both expressions and returns the value of the second expression. So, the value of (i,j) is 10.
    - Since the value of (i,j) is not equal to 0, the ternary operator returns the value of i, which is 10.
    - Finally, the values of i and j are printed, which are both 10.

    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
  • Jun 11, 2018
    Quiz Created by
    Nchaudhuri31
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.