Open In App

C Language | Set 9

Last Updated : 13 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Following questions have been asked in GATE 2012 exam.
1. What will be the output of the following C program segment? 
 

C




char inchar = 'A';
switch (inchar)
{
case 'A' :
    printf ("choice A \n") ;
case 'B' :
    printf ("choice B ") ;
case 'C' :
case 'D' :
case 'E' :
default:
    printf ("No Choice") ;
}


(A) No choice 
(B) Choice A 
(C) Choice A 
Choice B No choice 
(D) Program gives no output as it is erroneous
Answer (C) 
There is no break statement in case ‘A’. If a case is executed and it doesn’t contain break, then all the subsequent cases are executed until a break statement is found. That is why everything inside the switch is printed. 
Try following program as an exercise. 
 

C




int main()
{
    char inchar = 'A';
    switch (inchar)
    {
    case 'A' :
        printf ("choice A \n") ;
    case 'B' :
    {
        printf ("choice B") ;
        break;
    }
    case 'C' :
    case 'D' :
    case 'E' :
    default:
        printf ("No Choice") ;
    }
}


2. Consider the following C program 
 

C




int a, b, c = 0;
void prtFun (void);
int main ()
{
    static int a = 1; /* line 1 */
    prtFun();
    a += 1;
    prtFun();
    printf ( "\n %d %d " , a, b) ;
}
  
void prtFun (void)
{
    static int a = 2; /* line 2 */
    int b = 1;
    a += ++b;
    printf (" \n %d %d " , a, b);
}


What output will be generated by the given code segment? 
(A) 3 1 
4 1 
4 2 
(B) 4 2 
6 1 
6 1 
(C) 4 2 
6 2 
2 0 
(D) 3 1 
5 2 
5 2
Answer (C) 
‘a’ and ‘b’ are global variable. prtFun() also has ‘a’ and ‘b’ as local variables. The local variables hide the globals (See Scope rules in C). When prtFun() is called first time, the local ‘b’ becomes 2 and local ‘a’ becomes 4. 
When prtFun() is called second time, same instance of local static ‘a’ is used and a new instance of ‘b’ is created because ‘a’ is static and ‘b’ is non-static. So ‘b’ becomes 2 again and ‘a’ becomes 6. 
main() also has its own local static variable named ‘a’ that hides the global ‘a’ in main. The printf() statement in main() accesses the local ‘a’ and prints its value. The same printf() statement accesses the global ‘b’ as there is no local variable named ‘b’ in main. Also, the default value of static and global int variables is 0. That is why the printf statement in main() prints 0 as value of b.
3. What output will be generated by the given code d\segment if: 
Line 1 is replaced by “auto int a = 1;” 
Line 2 is replaced by “register int a = 2;” 
(A) 3 1 
4 1 
4 2 
(B) 4 2 
6 1 
6 1 
(C) 4 2 
6 2 
2 0 
(D) 4 2 
4 2 
2 0
Answer (D) 
If we replace line 1 by “auto int a = 1;” and line 2 by “register int a = 2;”, then ‘a’ becomes non-static in prtFun(). The output of first prtFun() remains same. But, the output of second prtFun() call is changed as a new instance of ‘a’ is created in second call. So “4 2” is printed again. Finally, the printf() in main will print “2 0”. Making ‘a’ a register variable won’t change anything in output.
Please see GATE Corner for all previous year paper/solutions/explanations, syllabus, important dates, notes, etc.
Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads