FaltuTech.Club : Fane of Advanced Logical Thriving Utopian Technical Club

Control Instructions in C- Questions and Answers

1. In a switch statement 'default' case is written at the end or at the start or wherever we want inside switch?

— It can be written anywhere in switch control instruction as an independent case.
2. Is the below format of 'for' loop is correct?

 


 for(;;) 


 
 — Yes but it will create an infinite loop.
 
3. Is the following usage of 'while' is correct?

 

 


 while() { //various statements } 

 

 — No. 'while' loop needs condition.
 
4. Which of the following statements are correct about an if-else statement in a C program?

A) Every if-else statement can be replaced by an equivalent statement using '? :' operators.
B) Nested if-else statements are allowed.
C) If we use an if it is compulsory to use an else.
D) Multiple statements in an if block are allowed.
E) Multiple statements in an else block are allowed.

— B,D,E

5. What would following if statement evaluates to? True or False.

 


 int x=60, y=50;
	if(!(!x) && x) 

 

— True

6. Find an error in the following code snippet – 

 


 
	void fun();
	void main(){
		int i=1;
		while(i2)
				goto here;
		}	
	}
	
	void fun(){
	here:
	}
	

— 'goto' cannot take control to a function other than the one in which it is being use. In other words, 'goto' cannot perform a non-local jump.

 

8. What will be the output of the following –
A) if(printf("I am if"));
B) while(printf("I am while"));    

— A) I am if
   B) It will output infinite number of 'I am while' string.

9. Which of the following errors would be reported by the compiler on compiling the program given below –

 


  
	#include 
	int main(){
		int a=5;
		switch(a){
			case 1:
			..
			case 2:
			..
			case 3+5:
			..
			case a:
			..
		}
	return 0;	
	}

A. Variables cannot be checked using a switch as in case a 
B. Expression as in case 3+5 is not allowed
C. All cases in the switch are not unique
D. There is no break statement in each case
E. There is no continue statement in each case

 

— A,C
Note: 3+5 is a constant expression and will be accepted.

10. Can we use 'continue' inside switch body?

— No.

11. What will be the value of 'b' and 'c' in the following code?

 


 	int a=500,b=100,c;

		if(!a>=400)
			b=300;
		c=200;
		
		

— b=100 , c=200

12. Is there any error in following statements?

A. for(i=0;i<10;i++);
B. if(1!=2);

— No.

13. Is there any error in the following code – 

 


	int a=5;
	switch(a)
	{
	}
	

— No. There can exist a switch, which has no cases.

 

14. Rewrite the following set of statements using conditional operators.


	int a=1,b;
	if(a>10)
		b=20;
	

 


	int a=1,b,dummy;
	a>10 ? b=10: dummy =1;
	

    Note that the following would make the first ; work as a statement terminator and the second ; work as a null statement.


 a>10 ? b=20 : ;;
	

15. what will be the output of the following statement –

 

 


 for(i=0;i

 

-- The statement will output  0 1 2  ... 126 127

16. What is more efficient a switch statement or an if-else chain?

-- Both are equal in terms of efficiency. If the cases in a switch are sparsely distributed the compiler may internally use the equivalent of an if-else chain instead of a compact jump table. However, one should use switch where one can. It is definitely a cleaner way to program and certainly is not any less efficient than the if-else chain.

17. Can we use a switch statement to switch on strings.

-- No. The cases in a switch must either have integer constants or constant expressions.

18. The way 'break' is used to take the control out of 'switch' can 'continue' be used to take the control to the beginning of the 'switch'?

-- No. 'continue' can work only with loops and not with switch.

19. Are the following statements are equivalent - 
A. else if(a=b)
B. elseif(a=b)

-- No. 'elseif' is not a keyword in C.

20. Which one is greater -
A. float a=0.7
B. 0.7 (hard coded in program)

--B. Here 'a' is float, whereas 0.7 is a double. When 0.7 is stored in 'a' what get stored is a 32-bit binary equivalent of 0.7. the binary equivalent of 0.7 turns out to be a recurring  number. This recurring number is terminated at 32 bits when it is stored in a. As against this, when 0.7 is terminated at 64 bits. When the 64-bit recurring binary euivalent of 0.7 is compared with 32-bit recurring binary equivalentof 0.7, the 64-bit value turns out to greater than the 32-bit value. Hence the condition turns out to be true.

21. Is the following while statement is correct?

 


 while(x=0;x

 

-- There will be an error because 'while' loop cannot take the form of a 'for' loop.

22. Which of the following is not a logical operator?
A. &
B. && 
C. || 
D. !

-- A

23. Which of the following cannot be cheked in a 'switch' - case statement?
A. Character
B. Integer
C. Float
D. Enum

-- C

24. Which of the following statements are correct about a 'for' loop used in a C program?
A. 'for' loop works faster than a 'while' loop
B. Everything that can be done using a 'for' loop can also be done using a 'while' loop.
C. 'for(;;)' implements an infinite loop
D. 'for' loop can be used if we want statements in a loop to get executed at least once.
E. 'for' loop works faster than a 'do-while' loop.

-- B,C,D

25. Which of the following statements are correct about a 'switch' statement in a C program?
A. 'switch' is useful when we wish to check the value of variable against a particular set of values.
B. 'switch' is used when we wish to check whether a value falls in different ranges.
C. Compiler implements a jump table for cases used in a 'switch'.
D. It is NOT compulsory to use a 'break' in every 'switch' statement.
E. A common set of statements CANNOT be used for multiple cases in a 'switch'

-- A,C,D

26. What will be the output of following statements?


 int a=3;
	if(a=3.0)
		printf("Equal");
	else 
		printf("Unequal");
	

-- Equal.  
  During comparison 'x' would get promoted to a 'float' and then two 'floats' would be compared.

 
27. What will the compile time error of the following?

 

 


 
	int i=10,j=15;
	if(i%2=j%3)
		printf("Hello");
		

 

-- Error : 'Lvalue required'

28. How would you rewrite the following program using a 'while' and a 'for' loop?

 


 
	#include 
	int main()
	{
		char ch='Y';
		do{
			//some statements
			printf("Continue Y/N");
			fflush(stdin);
			scanf("%c",&ch);
		}
		while (ch=='Y');
	
	}

-- Using 'for' loop


		#include 
		int main(){
			char ch='Y';
			for(;;){
			//statements
			printf("Continue Y/N");
			fflush(stdin);
			scanf("%c",&ch);
			if(ch!='Y')
				break;
			}
		
		}

Using 'while' loop


	#include 
	int main(){
		char ch='Y';
		while(1){
		//statements
		printf("Continue Y/N");
		fflush(stdin);
		scanf("%c",&ch);
		if(ch!='Y')
			break;
		}
	
	}