Printf MCQ 4 : Two Variables are Declared but Not Specified

Printf Multiple Choice Questions  : Two Variables are Declared but Not Specified 


Predict the Output of the Following Program if Given Things are – 

  • Two Variables are declared .
  • Two Variables are initialized
#include<stdio.h>
void main()
{
int input1 = 10,input2 = 20;
printf("%d %d");
}

Options :

  1. 10 20
  2. 20 10
  3. 10 10
  4. 20 20

[toggle title=”Output”]20 10[/toggle]


Switch to Printf MCQ Home :  Click Here


How and Why ?

  1. Undefined Behavior of Printf
  2. Click Here to See All Observation of Undefined Behavior

Printf MCQ 3 : Variable is Declared but Not Specified in Printf

Printf Multiple Choice Questions  : Variable is Declared but Not Specified in Printf


Predict the Output of the Following Program ?

  • Programs Compiled on Borland CC++ 3.0
#include<stdio.h>
void main()
{
int ivar = 10;
printf("%d%d");
}

Options :

  1. 0 0
  2. Compile Error : Undefined Symbol
  3. 10 0
  4. 0 10

[toggle title=”Output”]10 0[/toggle]


Switch to Printf MCQ Home :  Click Here


How and Why ?

  1. Undefined Behavior of Printf
  2. Click Here to See All Observation of Undefined Behavior

Printf MCQ 2 : No Variable is Specified Printf(“%d”)

Printf MCQ 2 : No Variable is Specified in Printf


Predict the Output of the Following Program ?

  • All Programs are Compiled on Borland CC++ 3.0
#include<stdio.h>
void main()
{
printf("%d");
}

Options :

  1. Compile Error
  2. Run Time Error
  3. 0
  4. Garbage Value

[toggle title=”Output”]0[/toggle]


Switch to Printf MCQ Home :  Click Here


How and Why ?

  1. Printf behavior is Undefined when No Variable is Specified
  2. Click Here to See All Observation of Undefined Behavior

Floating number in switch case : MCQ

This tutorial will explain you another verity of switch case statement. What will happen if we use floating number in switch case statement ?

Floating number in switch case : MCQ

void main()
{
 float choice = 10.1 ;
 switch(choice)
  {
  case 10.1 :
            printf("\nCase 10.1");
            break;
  case 10.2 :
            printf("nCase 10.2");
            break;
  case 10.3 :
            printf("nCase 10.3");
            break;
  }
}

Options : Do you think program will compile ?

  1. Yes
  2. No

Explanation

First of all, each and every compile have its own precision defined. If we compare the two numbers without precision then it will always return true if two numbers are same

Consider the below program –

int num = 10;
if(num == 10) {
  printf("True");
}

It works in case of integer value, if we use floating point number then compiler have different representation format of constant value and floating point variable.

float num = 10.20;

if(num == 10.20) {
  printf("True");
} else {
  printf("False");
}

Above program always ends in false condition because compiler have two representations of float number. so actual comparison will be like this –

if(10.19999999999 == 10.20)

Same situation is happening when we use floating number in Switch case. so it is re-commanded to use constant integer expression in the switch case statement,

Recommended Tutorial : Rules of using switch case

Relational operators in switch case

In this tutorial we will be learning how to tackle c program if we have relational operators in switch case.

Relational operators in switch case

void main()
{
   int choice = 10 ;
   switch(choice) 
   {
    case <10:
         printf("Less than 10");
         break;

    case =10:
         printf("Equal to 10");
         break;

    case >10:
         printf("Greater than 10");
         break;
   }
}

Options :

  1. Yes
  2. No

Explanation

In C programming when you use relational operator in an expression then it will return you two results either true or false i.e 0 or 1.

Relational operators in switch case

Consider the below program which is expanded version after evaluating expression

void main()
{
   int choice = 10 ;
   switch(choice) 
   {
    case 0:
         printf("Less than 10");
         break;

    case 1:
         printf("Equal to 10");
         break;

    case 0:
         printf("Greater than 10");
         break;
   }
}

You will find the above expansion because here choice = 10 so only 2nd condition will be evaluated to be true so all the remaining cases will be having label 0.

Recommended Article : Switch Case statement in C | Rules of using switch case

In this case all other conditions will be having same switch case label so it will throw compile time error. So C does not allow us to use relational operators in switch case

Switch case MCQ 15: Characters are allowed

Switch case MCQ 15: Characters are allowed


Problem Statement 15 : Characters are allowed

void main()
{
 char choice = 'a' ;
 switch(choice)
 {
 case 'A' :
 case 'a' : 
        printf("\nA for Apple");
        break;
 case 'B' :
 case 'b' :
        printf("\nB for Ball");
        break;
 }
}

Options : Will this program works for character Variable

  1. Yes
  2. No

[toggle title=”Output”]Yes[/toggle]


Explanation :

  1. First Rule of Using Switch is that Integer are Characters are allowed .
  2. Generally Case Label Should be Integer / Constant , And Character can be converted into Integer (ASCII value)
  3. Thus above code is absolutely Correct .

C Programming MCQ – 1 [ Cannot Modify Constant Value ]

Question : What will be the Output of the Program.?


#include<stdio.h>
void main()
{
int  const * p=5;
printf("%d",++(*p));
}

Answer

Compiler error: Cannot modify a constant value


Explanation :

  1. p is a pointer to a “Constant Integer“.
  2. We tried to change the value of the “Constant Integer“.
  3. Value of the Constant Integer Cannot Be Modified.

Switch Case Puzzle : C Programming

In this tutorial we will try to solve the below Switch case puzzle. This Switch case puzzle is again explained in simple words.

C Programming Switch case puzzle

Guess the output of the following program –

#include<stdio.h>
int main()
{
    int a=10;
    switch(a)
    {
      case '1':
          printf("ONE\n");
          break;
      case '2':
          printf("TWO\n");
          break;
      defa1ut:
          printf("NONE\n");
    }
    return 0;
}

Output :

Compile error

Explanation

If you expect the output of the above program to be NONE, I would request you to check it out!!
Answer :

  1. Program Won’t be Compiled.
  2. Check Spelling  of “default”.
default

Character Array MCQ 14 : Printing String in Function Recursively

Character Array MCQ 14 : Printing String Recursively


What is the Output of the Following Program ?

#include<stdio.h>
int main()
{
int i=0;
printf("\nI am in Main");
for(i=0;i<10;i++)
   main();
return(0);
}

Options :
[arrowlist]

  • 1.I am in MainI am in Main—- Infinite Times
  • 2.I am in MainI am in Main—- 10 Times
  • 3.I am in MainI am in Main—- Until Internal Stack Overflows
  • 4.None Of These

[/arrowlist]
Output:


Switch to String MCQ Home : Click Here


Note:
  • Function Does Not Remember Values Initialized Inside Calling Function except Static Variable.
  • So After Printing Message , again main() Function gets Called Where again i becomes 0
  • Thus This Program Runs Forever , Theoretically This Program Prints Message Infinite Number of Times
  • While Calling Recursive Function Contents are Pushed onto Internal Stack When This Internal Stack Overflows Program stops
  • We can Terminate Program Execution by Pressing Ctrl+Pause/Break

Character Array MCQ 13 : Printing Initialized Array

Character Array Multiple Choice Question  13 : Printing Initialized Array


What is the Output of the Following Program ?

#include<stdio.h>
#include<string.h>

int main()
{
char str[2] = "A";

printf("n%c",&str[0]);
printf("t%s",str);

return(0);
}

Options :

  1. A Garbage
  2. Garbage A
  3. Garbage Garbage
  4. A A

Output:


Switch to String MCQ Home : Click Here


Note:
  • Refer Our Article : [Initializing String]
  • In this Type of Initialization “\0” is not Compulsory .
  • So This Initialization is Perfectly Legal. It will Prints “A”.
  • First Character is Also “A”.