Tuesday, February 11, 2020

1.3. Looping applications in C

Session 3 


1. How to make an infinity loop in C?
A. loop: ..... goto loop;
B. for(;;) { }
C. while(1) { }
D. All of the above
ANSWER: D

2. What is the output of this C code?
#include <stdio.h>
int main(){
int i = 3;
int l = i / -2;
int k = i % -2;
printf("%d %d\n", l, k);
return 0;
}
A) Compile time error
B) -1 1
C) 1 -1
D) Implementation defined
ANSWER: B
EXPLANATION: Initially l=3/-2 so l will have the value -1 as it cannot take decimal places. k=3%-2 which will have the remainder as 1.

3. What is the value of x in this C code?
#include <stdio.h>
void main() {
int x = 5 * 9 / 3 + 9;
}
A) 3.75
B) Depends on compiler
C) 24
D) 3
ANSWER: C
EXPLANATION: Use BODMAS method

4. If c is a variable initialised to 1, how many times will the following loop be executed?
while ((c > 0) && (c < 60)){
loop body
c ++;
}
A. 60
B. 59
C. 61
D. None of these
ANSWER: B

5. Consider following program fragment char c ='a' ;
while (c++ < = 'z')
putchar (xxx) ;
If required output is abcd....xyz, then xxx should be
A. c++
B. c-2
C. c-1
D. --c
ANSWER: C

6. What will be output of following c code?
#include<stdio.h>
extern int x;
int main(){
do{
do{
printf("%o",x);
}
while(!-2);
}
while(0);
return 0;
}
int x=8;
A. 6
B. 8
C. 9
D. 10
ANSWER: D
EXPLANATION: Here variable x is extern type. So it will search the definition of variable x. which is present at the end of the code. So value of variable x =8. There are two do-while loops in the above code. AS we know do-while executes at least one time even that condition is false. So program control will reach at printf statement at it will print octal number 10 which is equal to decimal number 8.
Note: %o is used to print the number in octal format.In inner do- while loop while condition is ! -2 = 0.In C zero means false. Hence program control will come out of the inner do-while loop. In outer do-while loop while condition is 0. That is again false. So program control will also come out of the outer do-while loop.

7. What will be output of following c code?
#include<stdio.h>
int main(){
int i=2,j=2;
while(i+1?--i:j++)
printf("%d",i);
return 0;
}
A. infinite loop
B. 0
C. 1
D. Compiler time error
ANSWER: C
EXPLANATION: Consider the while loop condition: i + 1 ? -- i : ++j.In first iteration:i + 1 = 3 (True)So ternary operator will return -–i i.e. 1 In c 1 means true so while condition is true. Hence printf statement will print 1

In second iteration:i+ 1 = 2 (True) So ternary operator will return -–i i.e. 0 In c zero means false so while condition is false. Hence program control will come out of the while loop.

8. What will be output of following c code?
#include<stdio.h>
int main(){
int x=011,i;
for(i=0;i<x;i+=3){
printf("Start ");
continue;
printf("End");
}
return 0;
}
A. Start Start Start
B. Start
C. End Start
D. End End End
ANSWER: A
EXPLANATION: 011 is octal number. Its equivalent decimal value is 9.So, x = 9. Then perform the loop.

9. A "switch" statement is used to
A. Switch between functions in a program
B. Switch from one variable to another variable
C. To choose from multiple possibilities which may arise due to different values of a single variable
D. All of above
ANSWER: C

10. What will be output of following c code?
#include<stdio.h>
int main(){
int i,j;
i=j=2,3;
while(--i&&j++)
printf("%d %d",i,j);
return 0;
}
A. 2 3
B. 1 3
C. Runtime Error
D. Compile error
ANSWER: B
EXPLANATION: i and j value initially takes the first value it is initialized to . ie 2. So i=j=2. Perform the loop.

11. What will be output of following c code?
#include<stdio.h>
int main(){
static int i;
for(++i;++i;++i) {
printf("%d ",i);
if(i==4) break;
}
return 0;
}
A. 2 3
B. 2 4
C. 3 3
D. 1 1
ANSWER: B
EXPLANATION: Default value of static int variable in c is zero. So, initial value of variable i = 0
First iteration:
For loop starts value: ++i i.e. i = 0 + 1 = 1
For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf statement will print 2 
Loop incrimination: ++i i.e. i = 2 + 1 =3
Second iteration:
For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loop condition is true. Hence printf statement will print 
Since is equal to for so if condition is also true. But due to break keyword program control will come out of the for loop.

12. What will be output of following c code?
#include<stdio.h>
int main(){
int i=1;
for(i=0;i=-1;i=1) {
printf("%d ",i);
if(i!=1) break;
}
return 0;
}
A. Compile time error
B. 1
C. -1
D. Run time error
ANSWER: C
EXPLANATION:Initial value of variable i is 1.
First iteration:
For loop initial value: i = 0.For loop condition: i = -1 . Since -1 is non- zero number. So loop condition true. Hence printf function will print value of variable i i.e. -1
Since variable i is not equal to 1. So, if condition is true. Due to break keyword program control will come out of the for loop.

13. What is the output of this C code?
#include <stdio.h>
void main(){
int a = 3;
int b = ++a + a++ + --a;
printf("Value of b is %d", b);
}
A) Value of x is 12
B) Value of x is 13
C) Value of x is 10
D) Undefined behaviour
ANSWER: D

14. The precedence of arithmetic operators is (from highest to lowest)
A) %, *, /, +, –
B) %, +, /, *, –
C) +, -, %, *, /
D) %, +, -, *, /
ANSWER: A

15. What is the output of this C code?
#include <stdio.h>
int main(){
int a = 10;
double b = 5.6;
int c;
c = a + b;
printf("%d", c);
}
A) 15
B) 16
C) 15.6
D) 10
ANSWER: A

16. The following program fragment
for (i = 1; i< 5; ++ i )
if ( i == 3) continue;
else printf( " %d " i );
results in the printing of
A. 1 2 4 5
B. 1 2 4
C. 2 4 5
D. none of the above
ANSWER: B
EXPLANATION: The use of continue statement forces the execution to skip the remainder of the current pass over the loop and initiates the next. If ' U ' is 3. print f statement will be skipped. Hence the answer is b

17. What is the output of this C code?
#include <stdio.h>
void main(){
int x = 1, y = 0, z = 5;
int a = x && y || z++;
printf("%d", z);
}
A) 6
B) 5
C) 0
D) Varies
ANSWER: A

18. What is the output of this C code?
#include <stdio.h>
void main(){
int x = 1, z = 3;
int y = x << 3;
printf(" %d\n", y);
}
A) -2147483648
B) -1
C) Run time error
D) 8
ANSWER: D

19. What will be output of following c code?
#include<stdio.h>
int main(){
for(;;) {
printf("%d ",10);
}
return 0;
}
A. 10
B. infinite loop
C. sytanx error
D. Compile time error
ANSWER: B

20. What will be output of following c code?
#include<stdio.h>
int r();
int main(){
for(r();r();r()) {
printf("%d ",r());
}
return 0;
}
int r(){
int static num=7;
return num--;
}
A. 5 2
B. 5 4
C. 4 4
D. 5 3
ANSWER: A
EXPLANATION: First iteration:
Loop initial value: r() = 7
Loop condition: r() = 6
Since condition is true so printf function will print r() i.e. 5
Loop incrimination: r() = 4
Second iteration:
Loop condition: r() = 3
Since condition is true so printf function will print r() i.e. 2
Loop incrimination: r() = 1
Third iteration:
Loop condition: r() = 0
Since condition is false so program control will come out of the for loop.

21. What will be the value of d in the following program?
#include <stdio.h>
int main(){
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf("%d", d);
}
A) Syntax error
B) 1
C) 5
D) 10
ANSWER: B
EXPLANATION: b+c is evaluated and checked whether it is equal to a

22. Which among the following is NOT a logical or relational operator?
A) !=
B) ==
C) ||
D) =
ANSWER: D

23. What is the output of this C code?
#include <stdio.h>
int main(){
int a = 10;
if (a == a--)
printf("TRUE 1\t");
a = 10;
if (a == --a)
printf("TRUE 2\t");
}
A) TRUE 1
B) TRUE 2
C) TRUE 1 TRUE 2
D) Compiler Dependent
ANSWER: D
EXPLANATION: As it is sequence dependent, it depends on the sequence in which they are implemented

24. Relational operators cannot be used on:
a) structure
b) long
c) strings
d) float
ANSWER: a

25. What is the output of this C code?
#include <stdio.h>
int main(){
int y = 0;
if (1 |(y = 1))
printf("y is %d\n", y);
else
printf("%d\n", y);
}
A) y is 1
B) 1
C) Run time error
D) Undefined
ANSWER: A

26. What will be output of following c code?
#include<stdio.h>
char _x_(int,...);
int main(){
char (*p)(int,...)=&_x_;
for(;(*p)(0,1,2,3,4); )
printf("%d",!+2);
return 0;
}
char _x_(int a,...){
static i=-1;
return i+++a;
}
... INSIDE FUNCTION REPRESENTS NUMBER OF ARGUEMENTS
A. 1
B. -1
C. 0
D. 2
ANSWER: C
EXPLANATION: In c three continuous dot represents variable number of arguments.
p is the pointer to the function _x_
First iteration of for loop:
Initial value: Nothing // In c it is optional
Loop condition: (*p)(0,1,2,3,4)
= *(&_x_)(0,1,2,3,4) // p = &_x_
= _x_(0,1,2,3,4) //* and & always cancel to each other
= return i+++a
= return i+ ++a
= return -1 + 1
= 0
Since condition is false. But printf function will print 0. It is bug of c language.

28. What will be output of following c code?
#include<stdio.h>
int main(){
int i;
for(i=10;i<=15;i++){
while(i){
do{
printf("%d ",1);
if(i>>1)
continue;
}while(0);
break;
}
}
return 0;
}
A. 1 1 1 1
B. 1 1 1 1 1
C. 1 1 1 1 1 1
D. 1 1 1
ANSWER: C
EXPLANATION: For loop will execute six times. Note: continue keyword in do-while loop bring the program its while condition (while(0)) which is always false.

29. How many times this loop will execute?
#include<stdio.h>
int main(){
char c=125;
do
printf("%d ",c);
while(c++);
return 0;
}
A. Finite times
B. error
C. Infinite times
D. 0
ANSWER: C
EXPLANATION: If we will increment the char variable c it will increment as:
126,127,-128,-127,126 . . . . , 3, 2, 1, 0. When variable c = 0 then loop will terminate.

30. What will be output of following c code?
#include<stdio.h>
int main(){
int x=123;
int i={
printf("c" "++")
};
for(x=0;x<=i;x++){
printf("%x ",x);
}
return 0;
}
A. c++ 0 1
B. c++ (undefined)
C. c++ 0 1 2 3
D. runtime error
ANSWER: C
EXPLANATION: First printf function will print: c++ and return 3 to variable i. For loop will execute three time and printf function will print 0, 1, 2, 3 respectively.

31. What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int check=2;
switch(check){
case 1: printf("D.W.Steyn");
case 2: printf(" M.G.Johnson");
case 3: printf(" Mohammad Asif");
default: printf(" M.Muralidaran");
}
}
Choose all that apply:
A) M.G.Johnson
B) M.Muralidaran
C) M.G.Johnson Mohammad Asif M.Muralidaran
D) Compilation error
E) None of the above
ANSWER: C
EXPLANATION: If we will not use break keyword in each case the program control will come in each case after the case witch satisfy the switch condition.

32. What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int movie=1;
switch(movie<<2+movie){
default:printf("3 Idiots");
case 4: printf(" Ghajini");
case 5: printf(" Krrish");
case 8: printf(" Race");
}
}
Choose all that apply:
A) 3 Idiots Ghajini Krrish Race
B) Race
C) Krrish
D) Ghajini Krrish Race
E) Compilation error
ANSWER: B

33. What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int const X=0;
switch(5/4/3){
case X: printf("Clinton");
break;
case X+1:printf("Gandhi");
break;
case X+2:printf("Gates");
break;
default: printf("Brown");
}
}
Choose all that apply:
A) Clinton
B) Gandhi
C) Gates
D) Brown
E) Compilation error
ANSWER: E
EXPLANATION: Case expression cannot be constant

34. What will be output when you will execute following c code?
#include<stdio.h>
enum actor{
SeanPenn=5,
AlPacino=-2,
GaryOldman,
EdNorton
};
void main(){
enum actor a=0;
switch(a){
case SeanPenn: printf("Kevin Spacey");
break;
case AlPacino: printf("Paul Giamatti");
break;
case GaryOldman:printf("Donald Shuterland");
break;
case EdNorton: printf("Johnny Depp");
}
}
Choose all that apply:
A) Kevin Spacey
B) Paul Giamatti
C) Donald Shuterland
D) Johnny Depp
E) Compilation error
ANSWER: D
EXPLANATION: Default value of enum constant
GaryOldman = -2 +1 = -1
And default value of enum constant
EdNorton = -1 + 1 = 0
Note: Case expression can be enum constant.

35. What is the output of this C code?
#include <stdio.h>
void main(){
int k = 8;
int x = 0 == 1 && k++;
printf("%d%d\n", x, k);
}
A) 0 9
B) 0 8
C) 1 8
D) 1 9
ANSWER: B

36. What is the output of this C code?
#include <stdio.h>
void main(){
char a = 'a';
int x = (a % 10)++;
printf("%d\n", x);
}
A) 6
B) Junk value
C) Compile time error
D) 7
ANSWER: C

37. What is the output of this C code?
#include <stdio.h>
int main(){
int x = 2, y = 2;
x /= x / y;
printf("%d\n", x);
return 0;
}
A) 2
B) 1
C) 0.5
D) Undefined behaviour
ANSWER: A

38. Which of the following is an invalid assignment operator?
A) a %= 10;
B) a /= 10;
C) a |= 10;
D) None of the mentioned
ANSWER: D

39. What will be output when you will execute following c code?
#include<stdio.h>
void main(){
char *str="ONE"
str++;
switch(str){
case "ONE":printf("Brazil");
break;
case "NE": printf("Toy story");
break;
case "N": printf("His Girl Friday");
break;
case "E": printf("Casino Royale");
}
}
Choose all that apply:
A) Brazil
B) Toy story
C) His Girl Friday
D) Casino Royale
E) Compilation error
ANSWER: E
EXPLANATION: Case expression cannot be string constant

40. What will be output when you will execute following c code?
#include<stdio.h>
void main(){
switch(5/2*6+3.0){
case 3:printf("David Beckham");
break;
case 15:printf("Ronaldinho");
break;
case 0:printf("Lionel Messi");
break;
default:printf("Ronaldo");
}
}
Choose all that apply:
A) David Beckham
B) Ronaldinho
C) Lionel Messi
D) Ronaldo
E) Compilation error
ANSWER: E
EXPLANATION: In c switch expression must return an integer value. It cannot be float, double or long double

41. What will be output when you will execute following c code?
#include<stdio.h>
#define TRUE 1
void main(){
switch(TRUE){
printf("learn with durga");
}
}
Choose all that apply:
A) learn with durga
B) It will print nothing
C) Runtime error
D) Compilation error
E) None of the above
ANSWER: B
EXPLANATION: In c it is possible a switch case statement without any case but it is meaning less.

42. What will be output when you will execute following c code?
#include<stdio.h>
void main(){
static int i;
int j=1;
int arr[5]={1,2,3,4};
switch(arr[j]){
case 1: i++;break;
case 2: i+=2;j=3;continue;
case 3: i%=2;j=4;continue;
default: --i;
}
printf("%d",i);
}
Choose all that apply:
A) 0
B) 1
C) 2
D) Compilation error
E) None of the above
ANSWER: D
EXPLANATION: We cannot use continue keyword in switch case. It is part loop.

43. In C programming language, which of the following type of operators have the highest precedence
A. Relational operators
B. Equality operators
C. Logical operators
D. Arithmetic operators
ANSWER: D

44. What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int x=3;
while(1){
switch(x){
case 5/2: x+=x+++x;
case 3%4: x+=x---x;continue;
case 3>=3: x+=!!!x;break;
case 5&&0:x+=~~~x;continue;
default: x+=-x--;
}
break;
}
printf("%d",x);
}
Choose all that apply:
A) 3
B) -1
C) 5
D) Compilation error
E) None of the above
ANSWER: B

45. What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=3,b=2;
a=a==b==0;
switch(1){
a=a+10;
}
sizeof(a++);
printf("%d",a);
}
Choose all that apply:
A) 10
B) 11
C) 12
D) 1
E) Compilation error
ANSWER: D
EXPLANATION: Consider on the expression:
a=a==b==0;
a=(a==b)==0; //Since associate is right to left
a =(3==2)==0
a=0==0
a=1
switch case will not affect the value of variable a.
Also sizeof operator doesn't affect the value of the any variable

46. What is the output of this C code?
#include <stdio.h>
void main(){
char *str = "";
do{
printf("hello");
} while (str);
}
A) Nothing
B) Run time error
C) Varies
D) Hello is printed infinite times
ANSWER: D

47. Example of iteration in C.
A) for
B) while
C) do-while
D) All of the mentioned
ANSWER: D

48. Consider the following program fragment
d = 0;
for(i = 1; i < 31; ++i)
for(j = 1; j < 31; ++j)
for(k = 1; k < 31; ++k)
if(((i + j + k) % 3) == 0)
d = d + 1;
printf("%d", d );
The output will be

A. 9030
B. 27000
C. 3000
D. none of the above
ANSWER: A
EXPLANATION: a + b +c 3 will be 0 if a + b+c is a multiple of 3. This will happen in one of the following ways. All three - a, b, and c are multiples of 3. This can only happen if a, b, and c take one of the 10 values. - 3 , 6, 9 , ..... , 30, independent of one another. So, there are 10 x 10 x 10 = 1000 ways this can happen. Another possibility is that a, b, and c all leave a remainder 1 so that a + b + c is evenly divisible by 3. Considering all the different possibilities and adding. we get 9000. That will be the integer that gets printed.

49. Which keyword can be used for coming out of recursion?
A) break
B) return
C) exit
D) Both (a) and (b)
ANSWER: B

50. The output of the code below is
#include <stdio.h>
void main(){
int i = 0, k;
label: printf("%d", i);
if (i == 0)
goto label;
}
A) 0
B) Infinite 0
C) Nothing
D) Error
ANSWER: B

Previous                                Next

No comments:

Post a Comment