[go: up one dir, main page]

0% found this document useful (0 votes)
184 views20 pages

Practice Question Unit-1 and Unit-2 PDF

The document contains questions related to C programming concepts like variables, constants, data types, operators, conditional statements, loops, arrays, functions etc. It provides statements and asks to identify whether they are true or false. It also contains questions that ask to identify errors in code snippets, simplify logical expressions, and determine the output based on sample code executions.

Uploaded by

Anurag Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
184 views20 pages

Practice Question Unit-1 and Unit-2 PDF

The document contains questions related to C programming concepts like variables, constants, data types, operators, conditional statements, loops, arrays, functions etc. It provides statements and asks to identify whether they are true or false. It also contains questions that ask to identify errors in code snippets, simplify logical expressions, and determine the output based on sample code executions.

Uploaded by

Anurag Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Q: STATE WHETHER THE FOLLOWING STATEMENTS ARE TRUE OR FALSE:

1. Any valid printable ANSII character can be used in an identifier. ( False )


2. All variables must be given a type when they are declared. ( True )
3. Declarations can appear anywhere in a program. ( False )
4. ANSI C treats the variable name and Name to be same. ( False )
5. The underscore can be used anywhere in an identifier. ( True )
6. The keyword void is a data type in C. ( True )
7. Floating point data constants, by default, denote float type values. ( False )
8. Like variables, constants have a type. ( True )
9. Character constants are coded using double quotes. (False )
10. Initialization is the process of assigning a value to a variable at the time of declaration. (
true )
11. The scanf function can be used to read only one value at a time. ( False )

DECISION MAKING AND BRANCHING

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.

Fill in the blanks with appropriate words:


1. The keyword ……………..can be used to create a data type identifier. Answer: int
2. …………… is the largest value that an unsigned short int type variable can store.
Answer: 255
3. A global variable is also known as …………….variable. Answer: external
4. A variable can be made constant by declaring it with the qualifier ……………. at the time of
initialization. Answer: constant
5. The ……….operator is true only when both the operands are true. Ans: logical AND (&&).
6. Multiway section can be accomplished using an else if statement or
the ……………statement.
Ans: switch.
7. The……….. statement when executed in a switch statement causes immediate exit from
the structure. Ans: break.
8. The ternary conditional expression using the operator ?: code be easily coded using
………..statement. Ans: if…else.
9. The expression !(x!=y)can be replaced by the expression………… Ans: x==y.
10. In an exit controlled loop, if body is executed n times, test condition is evaluated times.
Ans: (n-1)
11. The statements is use to skip a part of the statements in a loop. Ans: continue.
12. A for loop with the no test condition is known as loop. Ans: infinite
13. The sentinel controlled loop is also; known as loop. Ans: indefinite repetition.
14. In a counter controlled loop, variable known as is used to count the loop operation. Ans:
definite repetition.
15. The variable used as a subscript in an array is popularly known as…… variable. Answer:
index.
16. An array can be initialized either at compile time or at ……….. Answer: run time.

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.

**count,price,city have values:


count=1275,
price=235.74,
city=cambridge.
what will be the output the statement ?
[a] printf(“%d %f”,count,price);
Output=1275 235.75.
[b] printf(“%d %f”,price,count);
Output=36576 790980
[c] printf(“%c”,city);
Output=cambridge

** Show the errors of the output statements.


[a] :printf(“%d.7.2%f”,year,amount);
7.2 should remain after the %
[b] :printf(“%-s,%c”\n,city,code);
comma is not allowed and \n should stay into quotation.
[c] printf(“%f %d %s”,price,count,city);
no error

** What values dose the computer assign of this input statements?


scanf(“%4d %*d”,&year,&code,&count);
if data kyed in 19883745
Output=1988.
**What is the purpose of scanf() function?
=if we want to take data after running the programme then we use scanf function.
** Describe the purpose of commonly used conversion characters in a scanf() function ?
it indicates what types of data we take as input.
** What happens when an input data item contain more characters than specified field width.
Value will be right-justified.
 fewer character than specified field width.
Value will be left-justefied.
** What is the purpose of printf() function ?
it is used to show anything on output.

**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.

Question: Find errors, if any, in the following declaration statements.


{
Intx;
float letter,DIGIT;
double=p,q;
exponent alpha,beta;
m,n.z:INTEGER
short char c;
long int m;count;
long float temp;
getch();
}
Error1: intx should have a type.
Error2: Line number 6, expression syntax.
Error3: Line number 7, Declaration should be properly.
Error4:Line number 9, Unreachable code.

** 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;}

Simplify the following compound logical expressions:


(a) !(x<=10) (b)!(x==10)||!((y==5)||(z<0))
Ans:(x>10) Ans: (x>0)
(c)!((x+y==z)&&!(z>5)) (d)!((x<=5)&&(y==10)&&(z<5))
Ans: (x<z) Ans: (x>5)

** 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

RQ-5.11:Find the error ,if any,in the following statements:


Solution:
(a)if(x>=10)
printf("\n");
Ans: No error.
(b)if(x>=10)
printf("OK");
Ans: No error.
(c)if(x==10)
printf ("Good");
Ans : No error.
(d) if(x=<10)
printf ("Welcome");
Ans : Error.
Correct ans: if (x<=10)
printf(“Welcome”);

** What is the output of the following program?


Program:
main()
{
int m;
for(m=1; m<5; m++)
printf("%d\n",(m%2) ? m : m*2);
getch();
}
Output:
1 4 3 8

** What is the output of following program?


Program:

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

** What will be the value of x when the following segment is executed?


int x=10,y=15;
x= (x<y)? (y+x) : (y-x) ;

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

** What will be the output when the following segment is executed?


Program:
char ch = ‘a’
switch(ch)
{
case ‘a’:
printf(“A”);
case ‘b’:
printf(“B”);
case ‘c’:
printf(“C”);
}
Output:
a
A
b
B
c
C

** What will be the output of the following segment when executed?


Program:
main()
{
int x=10,y=20;
if(( x<y)|| (x+5)>10)
printf("%d",x);
else
printf("%d",y);
getch();
}
Output:
10

** What will be the output of the following segment when executed?


Program:
main()
{
int a=10, b=5;
if(a>b)
{
if(b>5)
printf("%d",b);
}
else
printf("%d",a);
getch();
}
Output:
10

what would be the output of each of the following code segments?

(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;

…………………..

…………………..

Ans: Infinity times

(b) m=1;

do

……………………

……………………….

m+=2;

while(m<10)
Ans: 5 times.

(c) int i;

for(i=0;i<=5;i=i+2/3)

…………………..

…………………….

Ans: Infinity times.

(d) int m=10;

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);

Correct Ans: while(count!=10)

(b) name=0;

do

name+=1;

printf(“my name is Dinar\n”);

while(name=1);

Error: while (name=1);

Correct Ans: 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);

Correct ans: for(p=10;p>0;)

p-=1;

printf(“%f”,p);

Write a for statement to pront each of the following sequence of integers:

(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);

6.14: Change the following for loops to while loops :

(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”);

What is the output of following code?

Int m=100,n=0;

while(n==0)

if(m<10)

break;

m=m-10;

Output: No output

What is output of the following code?


int m=0;

do

if(m>10)

continue;

m=m+10;

while(m<50);

printf(“%d”,m);

Output: 50

What is the output of the following code?

int n=0,m=1;

do

printf(“m”);

m++;

while(m<=n);

Output: 1

What is the output of the following code?

int n=0,m;

for(m=1;m<=n+1;m++)

printrf(“m”);
Output: 1

When do we use the following statement?

for(; ;)

Ans : When we need an infinity loop the statement for(; ;) can be used.

Find errors, if any, in each of the following segments:

Solution:

(a)if((x+y=z) && (y>0) )

printf(" ");

Ans: Error.

Correct ans: if((x+y==z) && (y>0) )

printf(" ");

(b) if (code >1)

a= b+c

else

a=0

Ans: Error.

Correct ans: if (code >1)

a= b+c;

else

a=0;

(c) if(p>0) || (q <0)

printf("Sign is negative”);

Ans: Error.
Correct ans:if((p>0) || (q <0))

printf("Sign is negative”);

The following is a segment of a program:

x=1;

y=1;

if(n>0)

x=x+1;

y=y-1;

printf("%d %d", x,y);

what will be the values of x and y if n assumes a value of (a) 1and (b) 0.

Solution:

(a)The value of x is 2 & y is 0.

(b)The value of x & y is imaginary.

Rewrite each of the following without using compound relations:

(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

printf (“Not admitted”);

Solution:

if (M1>60)

printf (“Admitted\n”);

if (M2>60)

printf (“Admitted\n”);

else if(T>200)

printf (“Admitted\n”);

else

printf (“Not admitted”);

You might also like