Computer 12 Class Notes (MCQS)
Computer 12 Class Notes (MCQS)
(a) 0 (b) 3
(c) 6 (d) 5
30. The float data type variable has a precision of:
(a) 15 (b) 6
(c) 3 (d) 5
31. The double data type variable has precision of:
(a) 15 (b) 6
(c) 3 (d) 5
32. The float data type variable has a range of:
(a) 10−30 to 1030 (b) 10−300 to 10300
(c) 10−38 to 1038 (d) 10−308 to 10308
33. The double data type variable has a range of:
(a) 10−30 to 1030 (b) 10−300 to 10300
(c) 10−38 to 1038 (d) 10−308 to 10308
34. Which of the following is used to declare a real type variable?
a) long b) real
c) float d) cha
35. How many bytes the int data type takes in memory
a) 1 b) 2
c) 4 d) 8
36. is arithmetic operator:
(a) = (b) ==
(c) % (d) &&
37. is valid character constant:
(a) a (b) “a”
(c) ‘a’ (d) None
38. is increment operator:
(a) + (b) =+
(c) ++ (d) --
39. is a modulus operator:
[Ch#9] Elements of C 263 Computer Science Part-II
(a) ! (b) %
(c) ++ (d) /
40. If a = 19, then after execution of the statement b = a %5; the value of b will be:
(a) 5 (b) 4
(c) 2.71 (d) 2
41. The value returned by expression 3+3*5 will be:
(a) 16 (b) 18
(c) 12 (d) 8
42. How many bytes the float data type takes in memory
a) 1 b) 2
c) 4 d) 8
43. How many bytes the char data type takes in memory
a) 1 b) 2
c) 4 d) 8
44. Which of the following is an arithmetic expression?
a) ab&&cd b) ab+cd
c) !ab d) ab>cd
45. data type offers the highest precision:
(a) float (b) long int
(c) long double (d) long float
46. How many logical operators are in C?
(a) 4 (b) 3
(c) 2 (d) 6
47. is arithmetic expression:
(a) a && b (b) a+b
(c) !a (d) a>b
48. +,-,/,% and * are:
(a) Relational operators (b) Logical operators
(c) Arithmetic operators (d) None
49. If x = 3, then after executing the statement “x = x − −;”, the value of x will be:
(a) 2 (b) 3
(c) 1 (d) 4
50. If simple assignment statement is “x = x + 2;”, then its equivalent compound
assignment statement is:
[Ch#9] Elements of C 264 Computer Science Part-II
(a) x ++ = 2 (b) x + = 2;
(c) x = + 2; (d) y = x + 2;
[Ch#9] Elements of C 265 Computer Science Part-II
51. The value of 15%2 is
a) 7 b) 7.5
c) 2 d) 1
52. What will be the value of ‘x’ after executing the following statements?
float y = 6.59;
int x = 2;
x = y;
(a) 2 (b) 6.59
(c) 6 (d) 7
53. What will be the value of ‘x’ after executing the following statements?
int x = 2;
x + = x ++;
(a) 4 (b) 5
(c) 3 (d) 6
54. The names used to represent variables, constants , types functions etc are called
a) Words b) Characters
c) Identifiers d) All of Above
55. There are ____ types of identifiers in C language
a) 2 b) 3
c) 4 d) 1
56. Which one is not an example of standard identifier
a) printf b) if
c) sqroot d) else
57. The reserve words cannot be
a) Used in program b) Redefined
c) Copied d) All of Above
58. Initialization of a variable refers to
a) Value at the time of declaration b) Value at any time with in the program
c) Value after the program execution d) All of Above
59. What will be the value of ‘y’ after executing the following statements?
int y = 3
y = y + (++y);
(a) 8 (b) 7
(c) 4 (d) 6
[Ch#9] Elements of C 266 Computer Science Part-II
60. is not a valid variable name:
(a) a123 (b) my name
(c) real (d) float
61. data types is used to store real value:
(a) float (b) double
(c) long double (d) All
62. Which of the following data can be stored by “int” type variable?
(a) “2564” (b) -56.25
(c) 487596 (d) None
63. statements is not valid:
(a) char char = ‘a’; (b) char ch = ‘a’;
(c) char ch = ‘9’; (d) char ch = ‘A’;
64. statements is valid for declaring variable(s):
(a) float height; (b) int x, y, z;
(c) double marks, total; (d) All
65. Assigning a value to a variable at the time of its declaration is called:
(a) Initializing (b) Assigning
(c) Declaring (d) Naming
66. Which one is a valid variable name
a) @home b) #home
c) _home d) home+
67. A variable can be declared for ____ data type
a) One b) Two
c) Three d) Multiple
68. statement is valid for initializing variable(s):
(a) float height = 5.5; (b) int x = 2, y = 5, z = 6;
(c) char ch = ‘D’; (d) All
69. is valid character constant:
(a) ‘A’ (b) ‘6’
(c) ‘$’ (d) All
[Ch#9] Elements of C 267 Computer Science Part-II
70. The ‘int’ data type variable has a range of:
(a) -32768 to +32767 (b) 0 to 256
(c) 0 to 65536 (d) None
71. An expression may consist of:
(a) operands (b) operators
(c) Both (a) and (b) (d) None
72. An arithmetic expression may consist of:
(a) variables (b) constants
(c) arithmetic operators (d) All
73. is not an arithmetic operator:
(a) + (b) /
(c) × (d) %
74. The value of expression 2%3 is:
(a) 2 (b) 3
(c) 0 (d) None
75. _________ defines a set of values and a set of operations on those values
a) Data b) Constant
c) Data Type d) None of Above
76. The predefined data types of C language are called
a) User defined data types b) Standard data types
c) Normal data types d) None of Above
77. Which one is not an example of standard data type of C language
a) int b) float
c) char d) real
78. Which range refers to unsigned int data type of C language
a) 0 to 32768 b) 0 to 32767
c) 0 to 65536 d) 0 to 255
79. The value of expression 11%3 is:
(a) 2 (b) 3
(c) 3,66 (d) None
[Ch#9] Elements of C 268 Computer Science Part-II
80. The value of expression 11%3*5 is:
(a) 2 (b) 10
(c) 15 (d) None
81. valid assignment statement:
(a) x = 10; (b) x = x+y*3;
(c) x = y; (d) All
82. is valid statement to add 1 to variable x:
(a) x++; (b) x = x +1
(c) x + = 1; (d) All
83. Which range refers to signed int data type of C language
a) -32768 to 32767 b) 0 to 32767
c) 0 to 65536 d) 0 to 255
84. Long double data type of C language requires ____ bytes of memory
a) 2 b) 4
c) 10 d) 8
85. has higher order of precedence:
(a) + (b) ()
(c) * (d) /
86. How many relational operators in C?
(a) 4 (b) 3
(c) 2 (d) 6
87. Which of the following is relational operator?
(a) = (b) ==
(c) % (d) &&
88. is not relational operator:
(a) != (b) ==
(c) = (d) <=
89. An expression that uses relational expression is represented by:
(a) Relational expression (b) Arithmetic expression
(c) Logical expression (d) None
[Ch#9] Elements of C 269 Computer Science Part-II
90. A relational expression may return value:
(a) True (b) False
(c) Both a & b (d) None
91. The true value return by relational expression is represented by:
(a) 0 (b) 1
(c) Less then zero (d) None
92. returns True If X =5 and Y = 10:
(a) X>=Y (b) X >Y
(c) X==Y (d) X!=Y
93. returns False if X = 5 and Y = 10:
(a) X<Y (b) X <= Y
(c) X>Y (d) X!=Y
94. operators is used to join two conditions:
(a) Relational operator (b) Arithmetic operator
(c) Assignment operator (d) Logical operator
95. is logical operator:
(a) AND (b) OR
(c) NOT (d) All of these
96. The logical NOT operator, denoted by !, is a:
(a) Ternary operator (b) Unary operator
(c) Binary operator (d) Bitwise operator
97. is logical operator:
(a) = (b) ==
(c) % (d) &&
98. logical operators is unary operator:
(a) && (b) ||
(c) ! (d) None
99. &&, || and ! are:
(a) Relational operators (b) Logical operators
(c) Arithmetic operators (d) None
[Ch#9] Elements of C 270 Computer Science Part-II
100. symbols is used for logical OR operator:
(a) && (b) ||
(c) ! (d) |
101. symbols is used for logical AND operator:
(a) && (b) ||
(c) & (d) !
102. symbols is used for logical NOT operator:
(a) && (b) ||
(c) ! (d) None
103. is equivalent to !(p>=q):
(a) p=q (b) p<q
(c) p>q (d) !p<q
104. not a logical operator:
(a) || (b) !
(c) && (d) >
105. returns True if X = 2 and Y =3:
(a) !(X >Y) (b) (X > Y) || (Y > 2)
(c) (X < Y) && (X>=2) (d) All of these
106. returns Falst if X = 2 and Y = 3:
(a) (X = = Y) || (Y > X ) (b) (X = = 2) && (Y > 2)
(c) (X < Y) && (X > 2) (d) !(Y >3)
107. is not a logical operator:
(a) || (b) &&
(c) != (d) !
Q.3 Write T for True and F for false Statements.
1. printf and scanf are standard identifiers. (T)
2. In C language, all variables must be declared before being used. (T)
[Ch#9] Elements of C 271 Computer Science Part-II
3. Standard data types are not predefined in C language. (F)
4. The double data type required 4 bytes memory. (F)
5. In scientific notation the exponent represent the value of the number and
(F)
mantissa represents the power to which it is raised.
6. The symbol for modulus operator is %. (T)
7. The symbol = is used to compare two values. (T)
8. Operator precedence determines the order of evaluation of the operators in
(T)
an expression.
9. For many compilers a C variable name can be up to 31 characters. (T)
10. C program can only use lower case letters in variable names. (F)
Q.4 What data type would you use to represent the following items?
Item Data type
Number of children at you school int
A letter grade on an exam char
The average marks of your class float
Q.5 Which of the following are valid variable name in C?
Sr No Identifier/ Name Valid/invalid Invalid Reason
1) income Valid
2) total marks Invalid Space is not allowed
3) double Invalid C reserve word
4) average-score Invalid Symbol is not allowed
5) room# Invalid Symbol is not allowed
6) _area Valid
7) no_of_students Valid
8) long Invalid Reserve Words
9) Item Valid
10) MAX_SPEED Valid
[Ch#9] Elements of C 272 Computer Science Part-II
Q.6 Let w, x, y and z\ are four float type variables. Let a, b, and c are variables of
int type. Then correct the following statements:
1) z=4.0w*y; z=4.0*w*y;
2) y=yz; y=y*z;
3) a=6b4; a=6*b*4;
4) c=3(a+b); c=3*(a+b);
5) z=7w+xy; z=7*w+x*y;
3) p=a/d; p=2/3; 0
5) b=d/a+d%a; b=3/2+3%2=1+1 2
6) y=c/d*a; y=1/3*2=0*2 0
[Ch#10] Input/Output 282 Computer Science Part-II
Answer:
[Ch#10] Input/Output 288 Computer Science Part-II
#include<stdio.h>
#include<conio.h>
void main()
{
int m, n;
clrscr();
printf(“\nContents before Execution\nm=%d\t\t\tn=%d \n”,m,n);
printf(“ Enter two numbers separated by comma”);
scanf(“%d %d”, &m, &n);
m = m + 10;
n = 5 * m;
printf(“\nContents after Execution \nm = %d\t\t\t n=%d \n”,m,n);
}
Output:
Contents before Execution
m= 25308 n = 72830
Enter two numbers separated by comma 10, 15
Contents after Execution
m = 20 n = 100
Q6. Character is input by the user and print ASCII code of character:
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf(“Enter a character “);
[Ch#10] Input/Output 289 Computer Science Part-II
ch=getche();
printf(“\nASCII code for %c is %d”, ch, ch);
}
Q7. Character is input by the user and print ASCII code of character use scnaf()
function:
Answer:
#include<stdio.h>
void main()
{
char ch;
printf(“Enter a character “);
scanf(“%c”, &ch);
printf(“\nASCII code for %c is %d”, ch, ch);
}
Q8. Show how the value -17.246 would be pinted using the formats %8.4f,
%8.3f, %8.2f, %8.1f, %8.0f, and %0.2f
Answer:
Statement Output(� represents spaces)
printf(“%8.4f”,-17.246); -17.2460
printf(“%8.3f”,-17.246); �-17.246
printf(“%8.2f”,-17.246); � �-17.25
printf(“%8.1f”,-17.246); � � �-17.2
printf(“%8.0f”,-17.246); � � � � �-17
printf(“%0.2f”,-17.246); -17.25
Q9. Assuming x (type double) is 21.335 y (type int) is 200 Show output of the
following statement on paper
Answer:
Statement Output(�represents spaces)
[Ch#10] Input/Output 290 Computer Science Part-II
(a) 5 (b) 6
(c) 0 (d) None
[Ch#12] Loop Constructors 359 Computer Science Part-II
36. What will be the output of the following code segment?
int n = 5;
while (n > 5)
n * = 10;
printf(“%d”, n );
(a) 5 (b) 10
(c) 50 (d) 0
37. What will be the output of the following code segment?
int m = 10, n = 100;
do
{
m = m + (n ++);
m ++ ;
}while (m < 10);
printf(“%d”, m );
(a) 10 (b) 110
(c) 111 (d) 112
38. What will be the output of the following code segment?
int x = 0;
for (x = 100; x = = 200; x ++);
printf(“%d”, x );
(a) 101 (b) 100
(c) 201 (d) 200
39. What will be the output of the following code segment?
int n = 0
for (;;)
{
if (n = = 10) break;
n++;
}
printf(“%d”, n );
(a) 0 (b) 10
[Ch#12] Loop Constructors 360 Computer Science Part-II
(c) 11 (d) 12
40. What is the final value of x when the code intx; for(x=0; x<10; x++) is run?
(a) 10 (b) 9
(c) 0 (d) 1
Q.3 Write T for true and F for false statements.
1. There is no difference between while and do-while loop. (F)
2. The body of while loop may or may not execute. (T)
3. The do-while loop always executes at least once. (T)
4. The var++ is an example of prefix increment operator. (F)
5. The condition of an infinite loop never becomes true. (F)
6. Initialization expression is option in for loop. (T)
7. The for(i = 1; i < = 10; i++); is an infinite loop. (F)
8. Loop is a decision making construct. (F)
9. A while loop can not be used in the body of a for loop. (F)
10. In type casting, a variable of one type behaves as the variable of (T)
another type temporarily.
Programs using loops:
Q4. To print and find the sum of 10 natural numbers (1+2+3+ -----) using for loop
Answer:
#include<stdio.h>
void main ( )
{
int n,sm=0;
for(n=1;n<=10;n++)
{
printf(“\n %d”,n);
sm=sm+n;
}
printf(“\n The sum of 10 natural numbers = %d”,sm);
}
Q5. Print and find the sum of 10 numbers which are multiple of 5 (5+10+15+ -----
--) using for loop
Answer:
#include<stdio.h>
void main ( )
{
int n, sm=0;
for(n=5;n<=50;n+=5)
[Ch#12] Loop Constructors 361 Computer Science Part-II
{
printf(“\n %d”,n);
sm=sm+n;
}
printf(“\n The sum of numbers = %d”, sm);
}
Q6. Program: Print and find the sum of 10 natural numbers (1+2+3+ ---------)
Using While loop
Answer:
#include<stdio.h>
void main ( )
{
int n=1,sm=0;
while(n<=10)
{
printf(“\n %d”,n);
sm=sm+n;
n++;
}
printf(“\n The sum of 10 natural numbers = %d”,sm);
}
Q7. Print and find the sum of 15 even numbers (2+4+6+ -----) using do-while loop
Answer:
#include<stdio.h>
void main ( )
{
int n=2,sm=0;
do
{
printf(“\n %d”,n);
sm=sm+n;
n+=2;
}
while(n<=30);
printf(“\n The sum of 15 even numbers = %d”,sm);
[Ch#12] Loop Constructors 362 Computer Science Part-II
}
Q8. Print the table of any number using for loop
Answer:
#include<stdio.h>
void main ( )
{
int n,c;
printf(“\n Enter table Number “);
scanf(“%d”, &n);
for(c=1;c<=10;c++)
{
printf(“\n %d x %d = %d”, n, c , n*c);
}
}
Q9. Print the table of any number using while loop
Answer:
#include<stdio.h>
void main ( )
{
int n,c;
printf(“\n Enter table Number “);
scanf(“%d”, &n);
c=1;
while(c<=10)
{
printf(“\n %d x %d = %d”, n, c ,n*c);
c++;
}
}
Q10. Print the Table using do-while loop
Answer:
#include<stdio.h>
void main ( )
{
int n, c;
[Ch#12] Loop Constructors 363 Computer Science Part-II
printf(“\n Enter table Number “);
[Ch#12] Loop Constructors 364 Computer Science Part-II
scanf(“%d”, &n);
c=1;
do
{
printf(“\n %d x %d = %d”, n , c, n*c);
c++;
}
while(c<=10);
}
Q11. Print the factorial of a number using do while
Answer:
#include<stdio.h>
void main ( )
{
int n,c=1;
printf(“\n Enter any Number 0 to 7 “);
scanf(“%d”, &n);
do
{
c=c*n;
n--;
}
while(n>=1);
printf(“\n The Factorial of given number = %d “, c);
}
Q12. Print the factorial of a number using for loop
Answer:
#include<stdio.h>
void main ( )
{
[Ch#12] Loop Constructors 365 Computer Science Part-II
int n,c=1,k;
printf(“\n Enter any Number 0 to 7 “);
scanf(“%d”, &n);
for(k=n;k>=1;k--)
{
c=c*k;
}
printf(“\n The Factorial of given number = %d “, c);
}
Q13. Trace the output of the program:
Answer:
Program Output ( � denotes blank spaces)
k=0; loop iteration = 6
while(k<=5) ( k=0,1,2,3,4,5)
{ k=0 ; output � �0 � 10
printf(“%3d%3d\n”, k , 10-k); k=1 ; output � �1 � �9
k++; k=2 ; output � �2 � �8
}
k=3 ; output � �3 � �7
k=4 ; output � �4 � �6
k=5 ; output � �5 � �5
Q14. Trace the output of the program
Answer:
Program Output ( _ denotes blank spaces)
j=10; loop iteration = 5
for(int i=1;i<=5; + + i) (i=,1,2,3,4,5)
{ i=1 ; output 1 _ 10
printf(“%d %d\n”, i, j); i=2 ; output 2 _ 8
j - = 2; i=3 ; output 3 _ 6
} i=4 ; output 4 _ 4
i=5 ; output 5 _ 2
[Ch#12] Loop Constructors 366 Computer Science Part-II
Q15. Trace the output of the program assuming m=3,n=5
Answer:
Program Output
for(k=1;k<=n; ++k) *
{ **
for(j=0; j<k; ++j) ***
{ ****
printf(“*”); *****
}
printf(“\n”);
}
Q16. Trace the output of the program assuming m=3,n=5
Answer:
Program Output
for(k=n;k>=0; --k) ***
{ ***
for(j=m; j>0; --j) ***
{ ***
printf(“*”); ***
}
printf(“\n”);
}
Q17. Correct the following code according to the instructions: Insert braces where
they are needed and correct errors if any. The corrected code should accept
five integers and should display their sum.
Answer:
incorrect code correct code
count = 0; int count=0, sum=0, next_num;
while(count<=5); count = 0;
count += 1; while(count<=5)
printf(“next number >”); {
scanf(“%d”, &next_num); count += 1;
next_num += sum; printf(“next number >”);
printf(“%d numbers were added; \n”); scanf(“%d”, &next_num);
printf(“their sum is %d. \n”, sum); sum += next_num;
}
printf(“%d numbers were added; \n”,
count);
[Ch#12] Loop Constructors 367 Computer Science Part-II
printf(“their sum is %d. \n”, sum);
Q18. Rewrite the following code segment using do-while loop.
Answer:
Code with do-while statement
sum=0; int sum=0,odd=1;
for(odd = 1; odd<n; odd = odd+2) do
sum = sum + odd; {
printf(“sum of the positive odd numbers less sum = sum + odd;
than %d is %d\n”,n, sum); odd = odd+2;
}
while(odd<=n);
printf(“sum of the positive odd numbers less
than %d is %d\n”,n, sum);
Q19. Prime Number
Answer:
#include<stdio.h>
void main ( )
{
int n, k;
printf(“\n Enter any Number :>>>> “);
scanf(“%d”, &n);
k=2;
while (k<=n-1)
{
if(n%k==0)
{
printf(“\n %d is not A Prime Number “, n);
break;
}
k++;
}
if (k == n)
[Ch#12] Loop Constructors 368 Computer Science Part-II
printf(“\n The Number is prime = %d “, n);
}
Q20. Print first 10 Natural Numbers, their squares and their cubes and calculate
the sum of these three series using for loop.
S1=1+2+3+-----+10, S2=12+22+32+-----+102, S3=13+23+33+-----+103
Answer:
#include<stdio.h>
void main( )
{
int i, s1=0,s2=0,s3=0;
for(i=1;i<=10;i++)
{
printf(“\n %d \t %d \t %d”,i,i*i,i*i*i);
s1=s1+I;
s2 = s2 + (i*i);
s3 = s2 + (i*i*i);
}
printf(“\ns1= %d \t s2=%d \t s3=%d”, s1,s2,s3);
}
Q21. Write a program that produces the following output
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
Answer:
#include<stdio.h>
void main ( )
{
int i, j;
for (j=0;j<=5;j++)
{
for(i=0;i<=j;i++)
printf(“%d\t”,i);
[Ch#12] Loop Constructors 369 Computer Science Part-II
printf(“\n”);
}
}
Q22. Write a program that produces the following output
0 1
1 2
2 4
3 8
4 16
5 32
6 64
Answer:
#include<stdio.h>
#include<math.h>
void main ( )
{
float j;
for (j=0;j<=6;j++)
printf(“\n%4.0f\t\t%4.0f”, j, pow(2,j) );
}
OR
#include<stdio.h>
void main ( )
{
int j, i = 1;
for (j=0;j<=6;j++)
{
printf(“\n%d\t%d”, j, i);
i *= 2;
}
}
Q23. Input and output of an array using scanf( )
Answer:
#include<stdio.h>
void main( )
{
int a[5],i;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element ”);
scanf(“%d”, &a[i]);
[Ch#12] Loop Constructors 370 Computer Science Part-II
}
for(i=0;i<=4;i++)
printf(“\n Array Element at %d index = %d ”,i, a[i]);
}
Q24. Input array elements and calculate sum
Answer:
#include<stdio.h>
void main( )
{
int a[5],i, s=0;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element ”);
scanf(“%d”, &a[i]);
s += a[i];
}
printf(“\n Sum of all Array Elements = %d ”, s);
}
Q25. Input array elements and sorting of an array.
Answer:
#include<stdio.h>
void main( )
{
int a[5], i, temp, j;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element ”);
scanf(“%d”, &a[i]);
}
for(i=1;i<=4;i++)
for(j=0;j<=3;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
[Ch#12] Loop Constructors 371 Computer Science Part-II
}
for(i=0;i<=4;i++)
printf(“\n Array Elements in order are : %d”, a[i]);
}
Q26. Input array elements and find largest number in an array.
Answer:
#include<stdio.h>
void main( )
{
int a[5], i, max;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element”);
scanf(“%d”, &a[i]);
}
max = a[0];
for(i=1;i<=4;i++)
if(a[i]>max)
max=a[i];
printf(“\n Largest Number in Array Elements = %d ”, max);
}
Q27. Input array elements and find smallest number in an array.
Answer:
#include<stdio.h>
void main( )
{
int a[5], i, min;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element”);
scanf(“%d”, &a[i]);
}
min=a[0];
for(i=1;i<=4;i++)
if(a[i]<min)
min=a[i];
[Ch#12] Loop Constructors 372 Computer Science Part-II
printf(“\n Smallest Number in Array Elements = %d ”, min);
}
[Ch#13] Functions 380 Computer Science Part-II
5. A user defined function can not be called in another user defined (F)
function.
8. Global variables exist in memory till the execution of the program. (T)