Practice Question Unit-1 and Unit-2 PDF
Practice Question Unit-1 and Unit-2 PDF
Practice Question Unit-1 and Unit-2 PDF
12. When if statements are nested , the last else gets associated with the nearest if without an
else. Ans: False.
13. One if can have more than one else clause. Ans: False.
14. A switch statement can always be replaced by a series of if..else statements. Ans: False.
15. A switch expression can be of any type. Ans: False.
16. A program stops its execution when a break statement is encountered. Ans: False.
17. Each expression in the else if must test the same variable. Ans: True.
18. Any expression can be used for the if condition. Ans: True.
19. Each case label can have only one statement. Ans: True.
20. The default case is required in the switch statement. Ans: True.
21. The predicate !( (x>=10) (y==5) ) is equivalent to (x<10) && (y!=5 ). Ans: True.
.
Decision Making and Looping
22. The do… while statement first executes the loop body and then evaluate the loop control
expression. Ans: True.
23. In a preset loop, if the body is executed n times, the test expression is executed n+1 times.
Ans: True.
24. The number of times a control variable is updated always equals the number of loop
iterations. Ans: True.
25. Both the preset loops include initialization within the statement. Ans: True.
26. In a for loop expression, the starting value of the control variable must be less than its
ending value. Ans: True.
27. The initialization, test condition and increment parts may be missing in a for statement.
Ans: False.
28. While loops can be used to replace for loops without any change in the body of the loop.
Ans: False.
29. An exit control loop is executed a minimum time even if the test condition false in the
iteration. Ans: False.
30. The use of continue statement considered as unstructured programming. Ans: True.
31. The three loop expressions used in a for loop header must be separated by commas.
Ans: True.
32. The type of all elements in an array must be the same. Answer: True.
33. When an array is declared, C automatically initializes its elements to zero. Answer: True.
34. An expression that evaluates to an integral value may be used as a subscript
Answer: True.
35. Accessing an array outside its range is a compile time error. Answer: True.
36. A char type variable cannot be used as a subscript in an array. Answer: True.
37. In C, by default, the first subscript is zero. Answer: True.
38. In declaring an array, the array size can be constant or variable or an expression
Answer: False.
39. The declaration int x[2]={1,2,3};is illegal. Answer: True.
Question:
Which of the following are invalid constants and why?
0.0001
Answer: (valid)
5×1.5
Answer: (Invalid)
Reason: Exponent must be an integer.
99999
Answer: Valid
Reason: Long integer.
+100
Answer: ( valid)
75.45E-2
Answer: ( Valid )
-45.6
Answer: ( Valid )
“15.75”
Answer: ( Invalid )
Reason: “” sign is not permitted.
-1.79e+4
Answer: (valid)
0.00001234
Answer: ( Valid )
Question: Which of the following are invalid variable and why?
Minimum
Answer: ( valid )
First.name
Answer: ( Invalid )
Reason:. Sign is not permitted.
N1+n2
Answer: ( Invalid )
Reason: + sign is not permitted.
&name
Answer: ( Invalid )
Reason: & is not permitted.
Doubles
Answer: ( Valid )
Reason: Keyword may be a part of variable name.
3rd_row
Answer: ( Invalid )
Reason: First character must be a letter or underscore.
n$
Answer: ( Invalid )
Reason: Dollar sign is not permitted.
Row1 ( Valid )
Float
Answer: ( Invalid )
Reason: float is a keyword.
Sum Total
Answer: ( Invalid )
Reason: White space is not permitted.
Row Total
Answer: ( Invalid )
Reason: White space is not permitted.
Column total
Answer: ( Invalid )
Reason: White space is not permitted
Q- State errors,if any, in the following statements.
[a] scanf(“%c %f %d”,city,&price,&year);
=no error.
[b] scanf(“%s %d”,city,amount);
there will be a & before amount.
[c] scanf(“%f %d”,&amount,&year);
=no error.
[d] scanf(\n”%f”,root);
=\n will remain into double quote.
[e] scanf(“%c %d %ld”,*code,&count,root);
=* is not allowed before code and &will stay before root.
* What will be the values stored of the variables year and code when the data 1988,x ?
[a] scanf(“%d %c”,&year,&code);
=year stors 1988 and code stors x.
[b] scanf(“%c %d”,&year,&code);
= year stors x and code stors 1988.
[c] scnaf(“%d %c”,&code,&year);
=code stores 1988 and year stores x.
**Assuming x=10 ,state whether the following logical expressions are true or false:
(a)x==10 && x>10 && !x Ans:False.
(b)x==10 || x> 10 && !x Ans:True.
(c)x==10 && x>10 ||!x Ans:False.
(d)x==10 ||x>10 || !x Ans:True.
** Find errors, if any, in the following switch related statements. Assume that the variables x and
y are of int type and x=1 and y=2.
Solution:
(a)switch(y);
Ans: Error.
Correct ans: switch(y)
(b)case 10;
Ans: Error.
Correct ans: case 10:
(c)switch(x+y)
Ans:No error.
(d)switch(x) {Case 2: y= x+y; break};
Ans: Error.
Correct ans: switch(x) {Case 2: y= x+y; break;}
** Assuming that x=5, y=0,and z=1 initially ,what will be their values after executing the
following code segments?
(a)if(x && y)
x=10;
else
y=10;
Output:
10
10
(b)if(x|| y ||z)
y=10;
else
z=0;
Output:
1
0
(c)if(x)
if(y)
z=10;
else
z=0;
Output:
10
0
(d)if(x ==0 || x && y)
if(!y)
z=0;
else
y=1;
Output:
0
1
RQ-5.10:Assuming that x=2,y=1 and z=0 initially ,what will be their values after executing the
following code segments?
(a)
switch(x)
{
case 2:
x=1;
y=x+1;
case 1:
x=0;
break;
default:
x=1;
y=0;
}
Output:
1
0
(b)
switch(y)
{
case 0:
x=0;
y=0;
case 2:
x=2;
z=2;
default:
x=1;
y=2;
}
Output:
0 0 0
main()
{
int m,n,p;
for(m=0; m<3;m++)
for(n=0;n<3;n++)
for(p=0;p<3;p++)
if(m+n+p==2)
goto print;
print:
printf("%d %d %d",m,n,p);
getch();
}
Output:
0 0 2
Solution:
The value of x after execution is :-25.
** What will be the output when the following segment is executed?
int x=0;
if(x>=0)
if(x>0)
printf("Number is positive");
else
printf("Number is negative");
Output:
0
Number is positive
1
Number is negative
(a)count=5;
while(count-- >0)
printf(“count”);
Output:
54321
(b)count=5;
while(-- count>0)
Printf(“count”);
Output:
4321
(c) count=5;
do printrf(“count”);
while(count>0)
Output:
54321
(d)for(m=10;m>7;m-=2)
printf(“m”);
output;
10 8
6.11:Analyse each of the program segment that follow the determine how many times the
body of each loop will be executed.
(a)x=5;
y=50;
while(x<=y)
x=y/x;
…………………..
…………………..
(b) m=1;
do
……………………
……………………….
m+=2;
while(m<10)
Ans: 5 times.
(c) int i;
for(i=0;i<=5;i=i+2/3)
…………………..
…………………….
Int n=7;
while(m%n>=0)
………………
m+=1;
n+=2;
…………….
Ans: 4 times.
6.12: Find errors, if any, in each of the following looping segments. Assume that all the
variables have been declared and assigned values.
(a)while(count!=10);
count=1;
sum+=x;
count+=1;
Error: while(count!=10);
(b) name=0;
do
name+=1;
while(name=1);
(c) do;
total+=value;
scanf(“%f”,&value);
while(value!=999);
Error: do;
Correct Ans: do
(E) m=1;
n=0;
for(p=10;p>0;)
p-=1;
printf(“%f”,p);
Error: for(p=10;p>0;)
p-=1;
printf(“%f”,p);
p-=1;
printf(“%f”,p);
(a) 1,2,4,8,16,32
Ans: for(i=1;i<=32;i=i*2)
printf(“%d”,i);
(b) 1,3,9,27,81,243
Ans: for(i=1;i<=243;i=i*i)
printf(“%d”,i);
(c) -4,-2,0,4
for(i=-4;i<=4;i=i+2)
printf(“%d”,i);
(d) -10,-12,-14,-18,-26,-42
for(i=-10;i<=-42;i=i-2)
printf(“%d”,i);
(a)for(m=1;m<10;m=m+1)
printf(“m”);
Ans: m=1;
while(m<10)
…………….
m++;
printf(“m”);
(b)for(;scanf(“%d”,&m)!=-1;)
printf(“m”);
Ans:
while(scanf(“%d”,&m)!=-1)
printf(“m”);
Int m=100,n=0;
while(n==0)
if(m<10)
break;
m=m-10;
Output: No output
do
if(m>10)
continue;
m=m+10;
while(m<50);
printf(“%d”,m);
Output: 50
int n=0,m=1;
do
printf(“m”);
m++;
while(m<=n);
Output: 1
int n=0,m;
for(m=1;m<=n+1;m++)
printrf(“m”);
Output: 1
for(; ;)
Ans : When we need an infinity loop the statement for(; ;) can be used.
Solution:
printf(" ");
Ans: Error.
printf(" ");
a= b+c
else
a=0
Ans: Error.
a= b+c;
else
a=0;
printf("Sign is negative”);
Ans: Error.
Correct ans:if((p>0) || (q <0))
printf("Sign is negative”);
x=1;
y=1;
if(n>0)
x=x+1;
y=y-1;
what will be the values of x and y if n assumes a value of (a) 1and (b) 0.
Solution:
(a) if(grade<=59&&grade>=50)
second=second+1;
Solution:
if(grade<=59)
second=second+1;
if(grade>=50)
second=second+1;
(b) if ( number>100||number<0)
printf(“Out of range”);
else
sum=sum+number;
Solution:
if ( number>100)
printf(“Out of range”);
else if(number<0)
printf(“Out of range”);
else
sum=sum+number;
(c) if (M1>60&&M2>60||T>200)
printf(“Admitted\n”);
else
Solution:
if (M1>60)
printf (“Admitted\n”);
if (M2>60)
printf (“Admitted\n”);
else if(T>200)
printf (“Admitted\n”);
else