EGN 3211 Chapter 4
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int check=4; switch(check){ case 1: printf("1\n"); case 2: printf("2\n"); case 3: printf("3\n"); } }
no output no case
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int x=8; do{ do{ printf("%o",x); } while(!-2); } while(0); }
10 binary
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int i; for(i=10;i<=15;i++){ while(i){ do{ printf("%d\n",i); if( i >1) continue; }while(0); break; } } }
10 11 12 13 14 15 for loop works, continue doesnt do anything, dowhile works w 0
What is the net effect of this C code? Choose the best answer void main(void) { int a = 4, n, i, result = a; scanf("%d", &n); for (i = 1 ;i < n; i++) result += a; printf("result is %d",result); } What operation pattern do you see? a) result contains multiplication of a and n b) result contains addition of a and n. c) result contains repeated multiplication d) result contains the value of a
A 4 gets added to itself n times
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int a=5; for(;;) { printf("%d ",a); if(!a--) break; } }
5 4 3 2 1 0 a-- until negative
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void) { int i=0; for(; i<=5; i++); printf("%d", i); }
6 ; after for i gets incremented to 5, and then the post ++ brings it to 6
We want to test whether a value lies in the range 2 to 4 or 5 to 7. Can we do this using a switch? A. Yes B. No
A switch(a) { case 2: case 3: case 4: /* some statements */ break; case 5: case 6: case 7: /* some statements */ break; }
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int i; for(i=0;i>=5;i++) printf("%d",i); }
no output condition is never true
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void) { int x; for(x=-1; x<=10; x++) { if(x < 5) continue; else break; printf("Orlando"); } }
no output continue skips rest of iteration, repeats loop, never prints
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void) { int i=0; for(; i<=5; i++){ printf("%d", i); ++i; } }
024 i gets ++ twice
Point out the error, if any in the for loop. #include<stdio.h> void main(void) { int i=1; for(;;) { printf("%d\n", i++); if(i>10) break; } }
1 2 3 4 5 6 7 8 9 10 could also be for(i;i<=10;i++)
Which of the following cannot be checked in a switch- case statement? A. Character B. Integer C. Float D. enum
C
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int check=3; switch(check){ case 3: printf("3\n"); case 1: printf("1\n"); break; case 0: printf("0\n"); default: printf("what ever\n"); } }
3 1 no break after 3
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int i=1; for(i=0;i=-1;i=1) { printf("%d ",i); if(i!=1) break; } }
-1 starts at -1, !=1, breaks
What is the outcome/output of the following C program, if any? /* Note the ; at the end of for statement */ #include<stdio.h> void main(void){ int i; for(i=0;i>=5;i++); printf("%d",i); }
0 ; after for assumes i is 0
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ switch(0X0){ case '0':printf("0"); break; case 0: printf("0"); break; default: printf("default"); } }
0 the second one
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int check=2; switch(check){ case 1: printf("1\n"); case 2: printf("2\n"); case 3: printf("3\n"); default: printf("what ever\n"); } }
2 3 whatever no break in between cases
Do logical operators in C language use short circuit? A. Yes B. No C. N/A. Circuit operation applicable only for hardware.
A?
Which of the following sentences are correct about a switch loop in a C program? 1: switch is useful when we wish to check the value of variable against a particular set of values. 2: switch is useful when we wish to check whether a value falls in different ranges. 3: Compiler implements a jump table for cases used in switch. 4: It is not necessary to use a break in every switch statement. A. 1,2 B. 1,3,4 C. 2,4 D. 2
B
Identify logical or syntax error in the for loop, if any. #include<stdio.h> int main(void) { int x=5; for(;;x++) { printf("%d\n", x); if(x>10) break; x++; } return 0; } A.for loop requires three components B.variable x is incremented twice C.it's an infinite loop. D. All the above E. No error
B 5 7 9 11 ++ twice, post happens after break
Break is used to take control out of switch and continue to take control of the beginning of the switch. A. Yes B. No
B continue only works with loops, not switch
Can we use a switch statement to switch on strings? A. Yes B. No
B must either have constants or constant expressions
Point out the error, if any in the program. #include<stdio.h> void main(void) { int P = 10; switch(P) { case 10: printf("Case 1\n"); case 20: printf("Case 2\n"); break; case P: printf("Case 2\n"); break; } } A. Error: No default value is specified B. Error: Constant expression required at line case P: C. Error: There is no break statement in each case. D. No error will be reported.
B outputs case 1 case 2
Which of the following errors would be reported by the compiler on compiling the program given below? #include<stdio.h> void main(void) { int a = 5; switch(a) { case 1: printf("First\n"); case 2: printf("Second\n"); case 3 + 2: printf("Third\n"); case 5: printf("Final\n"); break; } } A. There is no break statement in each case. B. Expression as in case 3 + 2 is not allowed. C. Duplicate case 'case 5': D. No error will be reported.
C
What is the net effect of this C code? Choose the best answer void main(void) { int a = 4, n, i, result = a; scanf("%d", &n); for (i = 1;i < n; i++) result *= a; printf("result is %d",result); } What operation pattern do you see? a) result contains multiplication of a and n b) result contains repeated multiplication c) result contains a raised to the power of n d) result contains the value of a
C 4 gets multiplied by itself n times
Point out the error, if any in the program. #include<stdio.h> void main(void) { int i = 1; switch(i) { case 1: printf("Case1\n"); break; case 1*2+4: printf("Case2\n"); break; } } A. Error: in case 1*2+4 statement B. Error: No default specified C. Error: in switch statement D. No Error
D case1
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void) { int i = 1; switch(i) { printf("Hello\n"); case 1: printf("Hi\n"); break; case 2: printf("\nBye\n"); break; } }
Hi hello is within the switch but has no true case
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ unsigned char c=280; switch(c){ printf("Start\t"); case 280:printf("Beckham\t"); case 24: printf("Messi\t"); default: printf("Ronaldo\t"); printf("End"); } }
Messi Ronaldo End ascii
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int x=123; int i= printf("c" "++"); for(x=0;x<=i;x++){ printf("%x ",x); } }
c++ 0 1 2 3 print statement returns 3 to i
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ switch(5/2*6+3.0){ case 3:printf("Beckham\n"); break; case 15:printf("Ronaldo\n "); break; case 0:printf("Messi\n"); break; default:printf("Pele\n"); } }
cant use float in switch statement
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ switch(0X0){ case '\0':printf("\0"); break; case 0: printf("0"); break; default: printf("default"); } }
compile error duplicate case
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int x=11,i; for(i=0;i<x;i+=3){ printf("Start: i is [%d]\n",i); continue; printf("End: i is [%d]\n",i); } }
start i is 0 3 6 9 never prints end bc continue
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int check='2'; switch(check){ case '1': printf(" symbol 1\n"); case '2': printf("symbol 2\n"); case '3': printf("symbol 3\n"); default: printf("what ever\n"); } }
symbol2 symbol3 whatever no break in between
What is the outcome/output of the following C program, if any? #include<stdio.h> void main(void){ int check=2; switch(check){ case '1': printf(" symbol 1\n"); case '2': printf("symbol 2\n"); case '3': printf("symbol 3\n"); default: printf("what ever\n"); } }
whatever single quotes: character ascii