C Programming Bit Bank U-1, U-2
C Programming Bit Bank U-1, U-2
(AUTONOMOUS)
NH-16, Chaitanya Knowledge City, Rajahmundry
Department of Computer Science & Engineering
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++)
if ((char)a[i] == '5')
printf("%d\n", a[i]);
else
printf("FAIL\n");
}
A. The compiler will flag an error
B. Program will compile and print the output 5
C. Program will compile and print the ASCII value of 5
D. Program will compile and print FAIL for 5 times
Answer: Option D
9. The format identifier „%i‟ is also used for _____ data type?
A. char
B. int
C. float
D. double
Answer: Option B
10. Which data type is most suitable for storing a number 65000 in a 32-bit system?
A. short
B. int
C. long
D. double
Answer: Option A
11. Which of the following is a User-defined data type?
A. typedef int Boolean;
B. typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
C. struct {char name[10], int age};
D. All of the mentioned
Answer: Option D
12. What is the size of an int data type?
A. 4 Bytes
B. 8 Bytes
C. Depends on the system/compiler
D. Cannot be determined.
Answer: Option C
13. What is the output of this C code?
int main()
{
char chr;
chr = 128;
printf("%d\n", chr);
return 0;
}
A. 128
B. - 128
C. Depends on the compiler
D. None of the mentioned
Answer: Option B
int main()
{
char c;
int i = 0;
FILE *file;
file = fopen("test.txt", "w+");
fprintf(file, "%c", 'a');
fprintf(file, "%c", -1);
fprintf(file, "%c", 'b');
fclose(file);
file = fopen("test.txt", "r");
while ((c = fgetc(file)) != -1)
printf("%c", c);
return 0;
}
A. a
B. Infinite loop
C. Depends on what fgetc returns
D. Depends on the compiler
Answer: Option A
15. What is short int in C programming?
A. Basic data type of C
B. Qualifier
C. short is the qualifier and int is the basic datatype
D. All of the mentioned.
Answer: Option C
16. Comment on the output of this C code?
int main()
{
float f1 = 0.1;
if (f1 == 0.1)
printf("equal\n");
else
printf("not equal\n");
}
A. equal
B. not equal
C. Output depends on compiler
D. None of the mentioned
Answer: Option B
17. Comment on the output of this C code?
int main()
{
float f1 = 0.1;
if (f1 == 0.1f)
printf("equal\n");
else
printf("not equal\n");
}
A. equal
B. not equal
C. Output depends on compiler
D. None of the mentioned
Answer: Option A
18. What is the output of this C code (on a 32-bit machine)?
int main()
{
int x = 10000;
double y = 56;
int *p = &x;
double *q = &y;
printf("p and q are %d and %d", sizeof(p), sizeof(q));
return 0;
}
A. p and q are 4 and 4
B. p and q are 4 and 8
C. Compiler error
D. p and q are 2 and 8
Answer: Option A
19. Which is correct with respect to size of the datatypes?
A. char > int > float
B. int > char > float
C. char < int < double
D. double > char > int
Answer: Option C
20. What is the output of this C code?
int main()
{
float x = 'a';
printf("%f", x);
return 0;
}
A. a
B. run time error
C. a.0000000
D. 97.000000
Answer: Option D
21. Which of the following is not a valid variable name declaration?
A. int __a3;
B. int __3a;
C. int __A3;
D. None of the mentioned.
Answer: Option D
22. Variable names beginning with underscore is not encouraged. Why?
A. It is not standardized
B. To avoid conflicts since assemblers and loaders use such names
C. To avoid conflicts since library routines use such names
D. To avoid conflicts with environment variables of an operating system
Answer: Option C
int main()
{
printf("Hello World! %d \n", x);
return 0;a
}
A. Hello World! x;
B. Hello World! followed by a junk value
C. Compile time error
D. Hello World!
Answer: Option C
29. What is the output of this C code?
int main()
{
int y = 10000;
int y = 34;
printf("Hello World! %d\n", y);
return 0;
}
A. Compile time error
B. Hello World! 34
Answer: Option A
30. Which of the following is not a valid variable name declaration?
A. float PI = 3.14;
B. double PI = 3.14;
C. int PI = 3.14;
D. #define PI 3.14
Answer: Option D
31. Which of the following cannot be a variable name in C?
A. Volatile
B. True
C. friend
D. export
Answer: Option A
32. What is the value of x in this C code?
void main()
{
int x = 4 *5 / 2 + 9;
}
A. 6.75
B. 1.85
C. 19
D. 3
Answer: Option C
33. What is the value of x in this C code?
void main()
{
int x = 4 *5 / 2 + 9;
}
A. 6.75
B. 1.85
C. 19
D. 3
Answer: Option C
34. MS-Word is an example of _____
A) An operating system
B) A processing device
C) Application software
D) An input device
Answer Option : C
35. Which of the following is a popular programming language for developing multimedia webpages.
A) COBOL
B) Java
C) BASIC
D) Assembler
Answer Option : B
Answer Option: B
37. The computer's processor consists of the following parts
A) CPU and Main Memory
B) Hard Disk and Floppy Drive
C) Control Unit and ALU
D) Operating System and Application
Answer Option: C
Answer Option: A
Answer Option: B
Answer Option: D
UNIT-II
1. What is the output of this C code?
int main()
{
int x = 1;
short int i = 2;
float f = 3;
if (sizeof((x == 2) ? f : i) == sizeof(float))
printf("float\n");
else if (sizeof((x == 2) ? f : i) == sizeof(short int))
printf("short int\n");
}
A. float
B. short int
C. Undefined behaviour
D. Compile time error
Answer: Option A
2. What is the output of this C code?
int main()
{
int a = 2;
int b = 0;
int y = (b == 0) ? a :(a > b) ? (b = 1): a;
printf("%d\n", y);
}
A. Compile time error
B. 1
C. 2
D. Undefined behaviour
Answer: Option C
3. What is the output of this C code?
int main()
{
int a = 0, i = 0, b;
for (i = 0;i < 5; i++)
{
a++;
if (i == 3)
break;
}
}
A. 1
B. 2
C. 3
D. 4
Answer: Option D
4. The keyword „break‟ cannot be simply used within:
A. do-while
B. if-else
C. for
D. while
Answer: Option B
5. Which keyword is used to come out of a loop only for that iteration?
A. break
B. continue
C. return
D. None of the mentioned
Answer: Option B
6. What is the output of this C code?
void main()
{
int i = 0, j = 0;
for (i = 0;i < 5; i++)
{
for (j = 0;j < 4; j++)
{
if (i > 1)
break;
}
printf("Hi \n");
}
}
A. Hi is printed 5 times
B. Hi is printed 9 times
C. Hi is printed 7 times
D. Hi is printed 4 times
Answer: Option A
7. What is the output of this C code?
int main()
{
printf("before continue ");
continue;
printf("after continue\n");
}
A. Before continue after continue
B. Before continue
C. after continue
D. Compile time error
Answer: Option D
#include <stdio.h>
void main()
{
int x = 5;
if (x < 1);
printf("Hello");
}
a) Nothing
b) Run time error
c) Hello
d) Varies
Answer: Option C
#include <stdio.h>
int main()
{
int x = 1;
if (x > 0)
printf("inside if\n");
else if (x > 0)
printf("inside elseif\n");
}
a) inside if
b) inside elseif
c)inside if
inside elseif
d) compile time error
Answer: Option A
#include <stdio.h>
int main()
{
int x = 0;
if (x++)
printf("true\n");
else if (x == 1)
printf("false\n");
}
a) true
b) false
c) compile time error
d) undefined behaviour
Answer: Option B
16. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int x = 0;
if (x == 0)
printf("true, ");
else if (x = 10)
printf("false, ");
printf("%d\n", x);
}
a) false, 0
b) true, 0
c) true, 10
d) compile time error
Answer: Option B
#include <stdio.h>
int main()
{
if (printf("%d", printf(")))
printf("We are Happy");
else if (printf("1"))
printf("We are Sad");
}
a) 0We are Happy
b) 1We are Happy
c) 1We are Sad
d) compile time error
Answer: Option D
19. What will be the output of the following C code? (Assuming that we have entered the value 1 in the
standard input)
#include <stdio.h>
void main()
{
double ch;
printf("enter a value between 1 to 2:");
scanf("%lf", &ch);
switch (ch)
{
case 1:
printf("1");
break;
case 2:
printf("2");
break;
}
}
a) Compile time error
b) 1
c) 2
d) Varies
Answer: Option A
#include <stdio.h>
int main()
{
float f = 1;
switch (f)
{
case 1.0:
printf("yes\n");
break;
default:
printf("default\n");
}
}
a) yes
b) yes default
c) Undefined behaviour
d) Compile time error
Answer: Option D
#include <stdio.h>
switch (ch)
{
case 'a':
case 'A':
printf("true");
}
a) if (ch == „a‟ && ch == „A‟) printf(“true”);
b)if (ch == 'a')
if (ch == 'a') printf("true");
c) if (ch == „a‟ || ch == „A‟) printf(“true”);
d) none of the mentioned
Answer: Option C
22. The C code „for(;;)‟ represents an infinite loop. It can be terminated by ___________
a) break
b) exit(0)
c) abort()
d) terminate
Answer: Option A
23. What will be the correct syntax for running two variable for loop simultaneously?
a) for (i = 0; i < n; i++)
for (j = 0; j < n; j += 5)
b) for (i = 0, j = 0; i < n, j < n; i++, j += 5)
c) for (i = 0; i < n;i++){}
for (j = 0; j < n;j += 5){}
d) none of the mentioned
Answer: Option B
24. Which for loop has range of similar indexes of „i‟ used in for (i = 0;i < n; i++)?
a) for (i = n; i>0; i–)
b) for (i = n; i >= 0; i–)
c) for (i = n-1; i>0; i–)
d) for (i = n-1; i>-1; i–)
Answer: Option D
25. Which of the following cannot be used as LHS of the expression in for (exp1;exp2; exp3)?
a) variable
b) function
c) typedef
d) macros
Answer: Option D
#include <stdio.h>
int main()
{
short i;
for (i = 1; i >= 0; i++)
printf("%d\n", i);
}
a) The control won‟t fall into the for loop
b) Numbers will be displayed until the signed limit of short and throw a runtime error
c) Numbers will be displayed until the signed limit of short and program will successfully terminate
d) This program will get into an infinite loop and keep printing numbers with no errors
Answer: Option D
#include <stdio.h>
void main()
{
int k = 0;
for (k)
printf("Hello");
}
a) Compile time error
b) hello
c) Nothing
d) Varies
Answer: Option D
28.What will be the output of the following C code?
#include <stdio.h>
void main()
{
int k = 0;
for (k < 3; k++)
printf("Hello");
}
a) Compile time error
b) Hello is printed thrice
c) Nothing
d) Varies
Answer: Option A
#include <stdio.h>
void main()
{
double k = 0;
for (k = 0.0; k < 3.0; k++)
printf("Hello");
}
a) Run time error
b) Hello is printed thrice
c) Hello is printed twice
d) Hello is printed infinitely
Answer: Option A
#include <stdio.h>
int main()
{
int i = 0;
for (; ; ;)
printf("In for loop\n");
printf("After loop\n");
}
a) Compile time error
b) Infinite loop
c) After loop
d) Undefined behaviour
Answer: Option A
#include <stdio.h>
int main()
{
while ()
printf("In while loop ");
printf("After loop\n");
}
a) In while loop after loop
b) After loop
c) Compile time error
d) Infinite loop
Answer: Option C
33.Which loop is most suitable to first perform the operation and then test the condition?
a) for loop
b) while loop
c) do-while loop
d) none of the mentioned
Answer: Option C
34. Which keyword is used to come out of a loop only for that iteration?
a) break
b) continue
c) return
d) none of the mentioned
Answer: Option B
39. Which loop is most suitable to first perform the operation and then test the condition?
a) for loop
b) while loop
c) do-while loop
d) none of the mentioned
Answer: Option C