Blank

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

1. Which of the following is the correct syntax to declare a variable in C?

A) `int 4num;`
B) `int num4;`
C) `int num-4;`
D) `int num 4;`

2. What will be the output of the following C code snippet?


```c
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
```
A) Hello, World!
B) hello, world!
C) HELLO, WORLD!
D) Compiler error

3. Which of the following data types is used to create a oating-point variable in C?


A) `int`
B) ` oat`
C) `char`
D) `double`

4. What will be the output of the following C code snippet?


```c
#include <stdio.h>
int main() {
int a = 5;
a = a + 10;
printf("%d", a);
return 0;
}
```
A) 5
B) 10
C) 15
D) 20

5. Which of the following is the correct way to write a comment in C?


A) `// This is a comment`
B) `/* This is a comment */`
C) Both A and B
D) None of the above

6. Which function is used to read a character from the standard input in C?


A) `printf()`
B) `scanf()`
C) `getchar()`
D) `putchar()`

7. What is the correct syntax to declare a pointer to an integer in C?


A) `int *p;`
B) `int p*;`
C) `*int p;`
D) `int p;`

8. What will be the output of the following C code snippet?


fl
fl
```c
#include <stdio.h>
int main() {
int x = 3;
int y = x++;
printf("%d", y);
return 0;
}
```
A) 2
B) 3
C) 4
D) Compiler error

9. Which of the following is the correct way to declare a constant variable in C?


A) `const int x = 10;`
B) `int const x = 10;`
C) Both A and B
D) None of the above

10. What is the output of the following code snippet?


```c
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
int c = a + b;
printf("%d", c);
return 0;
}
```
A) 10
B) 20
C) 30
D) 40

11. What will be the output of the following C code snippet?


```c
#include <stdio.h>
int main() {
int a = 5, b = 2;
oat c;
c = a / b;
printf("%f", c);
return 0;
}
```
A) 2.500000
B) 2.000000
C) 2
D) Compiler error

12. Which of the following is true about the scope of a variable declared inside a function in C?
A) It is global and can be accessed from anywhere in the program.
B) It is local to the function and cannot be accessed outside.
C) It is local to the le and can be accessed by other functions in the same le.
D) It is local to the block of code in which it is declared.

13. What does the following C code print?


```c
fl
fi
fi
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
*p = 20;
printf("%d\n", a);
return 0;
}
```
A) 10
B) 20
C) 0
D) Garbage value

14. Which of the following statements is correct about the `malloc` function?
A) `malloc` allocates memory and initializes it to zero.
B) `malloc` allocates memory but does not initialize it.
C) `malloc` allocates memory and initializes it to a speci c value provided by the user.
D) `malloc` does not allocate memory but only initializes it.

15. What will be the output of the following C code?


```c
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
printf("%d\n", *(arr + 3));
return 0;
}
```
A) 1
B) 2
C) 3
D) 4

16. What will be the output of the following code?


```c
#include <stdio.h>
void fun(int *arr, int n) {
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
}
int main() {
int arr[] = {10, 20, 30, 40};
fun(arr + 1, 3);
return 0;
}
```
A) 10 20 30
B) 20 30 40
C) 30 40
D) 20 30 40 10

17. Which of the following correctly describes the use of `typedef` in C?


A) To de ne a new data type.
B) To de ne a new name for an existing data type.
C) To declare a variable of a speci c data type.
D) To allocate memory dynamically for a variable.

18. What will be the output of the following code?


fi
fi
fi
fi
```c
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
printf("%c", *(str + 7));
return 0;
}
```
A) W
B) o
C) l
D) ,

19. Which of the following C statements is used to correctly read a string input from the user?
A) `scanf("%s", str);`
B) `scanf("%[^\n]s", str);`
C) `scanf("%c", str);`
D) `gets(str);`

20. What will be the output of the following C code?


```c
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("%p\n", p);
return 0;
}
```
A) Address of variable `a` in hexadecimal
B) Address of variable `a` in decimal
C) Value of variable `a`
D) Value of variable `p`

21. What will be the output of the following C code snippet?


```c
#include <stdio.h>
void main() {
oat a = 12.25, b = 13.65;
if (a == b)
printf("a and b are equal");
else
printf("a and b are not equal");
}
```
A) a and b are equal
B) a and b are not equal
C) Compiler error
D) Unde ned behavior

22. What will be the output of the following C code snippet?


```c
#include <stdio.h>
void main() {
oat a = 0.7;
if (a < 0.7)
printf("Stoned");
else
printf("Avenged");
}
fl
fl
fi
```
A) Stoned
B) Avenged
C) Compiler error
D) Unde ned behavior

23. What will be the output of the following C code snippet?


```c
#include <stdio.h>
void main() {
int x = 3, z;
z = x / ++x;
printf("x = %d z = %d\n", x, z);
}
```
A) x = 3 z = 1
B) x = 4 z = 0
C) x = 4 z = 1
D) x = 3 z = 0

24. What will be the output of the following C code snippet?


```c
#include <stdio.h>
void main() {
int i = 3;
switch (i) {
case 1: printf("cse\t");
case 2: printf("It\n"); break;
case 3: continue;
default: printf("goodbye");
}
}
```
A) cse It goodbye
B) cse It
C) Compiler error
D) goodbye

25. What will be the output of the following C code snippet?


```c
#include <stdio.h>
void main() {
int i = 2;
switch (i) {
case 0: printf("Zero\n"); break;
case 1: printf("One\n"); break;
case 2: printf("Two\n");
case 3: printf("Three\n"); break;
default: printf("Default\n");
}
}
```
A) Two
B) Three
C) Two Three
D) Default

26. Which of the following is not a valid variable name declaration?


A) int _a3;
B) int 3_a;
fi
C) int a_3;
D) int A3;

27. What is the purpose of the "static" keyword in C?


A) To restrict the scope of a variable to its le.
B) To allocate memory dynamically.
C) To create global variables.
D) To make a variable immutable.

28. Which of the following is the correct way to declare a function in C?


A) function int myFunc();
B) int myFunc();
C) declare myFunc();
D) int myFunc;

29. What is the output of the following code snippet?


```c
int main() {
int x = 5;
printf("%d", x++);
return 0;
}
```
A) 5
B) 6
C) Unde ned behavior
D) Compilation error

30. How do you allocate memory for an array of 10 integers using malloc?
A) `int *arr = malloc(10);`
B) `int *arr = malloc(10 * sizeof(int));`
C) `int arr[10];`
D) `int arr = malloc(10 * sizeof(int));`

31. Which of the following is true about a pointer in C?


A) It stores the memory address of another variable.
B) It can only store integer values.
C) It is a type of integer.
D) It cannot be used to access array elements.

32. What does the 'continue' statement do in a loop?


A) Terminates the loop.
B) Skips the current iteration and proceeds with the next iteration.
C) Exits the program.
D) Resets the loop.

33. Which of the following operators is used to access the value at the address stored in a pointer
variable?
A) &
B) *
C) ->
D) .

34. What is the size of a char data type in C?


A) 1 byte
B) 2 bytes
C) 4 bytes
D) 8 bytes

35. Which function is used to read a string from the standard input in C?
fi
fi
A) gets()
B) scanf()
C) fgets()
D) read()

36. How do you declare a pointer to a function that returns an int and takes a oat as an
argument?
A) `int (*ptr)( oat);`
B) `int *ptr( oat);`
C) `int ptr( oat);`
D) `int (*ptr) oat;`

37. What will be the output of the following code snippet?


```c
int main() {
int arr[] = {1, 2, 3, 4, 5};
printf("%d", *(arr + 2));
return 0;
}
```
A) 1
B) 2
C) 3
D) 4

38. Which library function is used to compare two strings in C?


A) strcpy()
B) strcmp()
C) strlen()
D) strcat()

39. What does the following declaration mean?


`int (*ptr)[10];`
A) ptr is a pointer to an array of 10 integers.
B) ptr is an array of 10 pointers to integers.
C) ptr is a pointer to an integer.
D) ptr is an integer array.

40. Which of the following is a correct statement to free allocated memory?


A) `free(&ptr);`
B) `free(*ptr);`
C) `free(ptr);`
D) `free(ptr[0]);`

41. How do you declare a constant variable in C?


A) `const int a;`
B) `constant int a;`
C) `int const a;`
D) Both A and C

42. Which header le is needed to use the printf() function?


A) `#include <string.h>`
B) `#include <math.h>`
C) `#include <stdio.h>`
D) `#include <stdlib.h>`

43. What is the correct syntax to de ne a macro in C?


A) `#de ne PI = 3.14`
B) `#de ne PI 3.14`
C) `#macro PI 3.14`
fi
fi
fl
fl
fl
fl
fi
fi
fl
D) `#macro PI = 3.14`

44. Which of the following function is used to write a character to the console in C?
A) write()
B) putchar()
C) puts()
D) printf()

45. What does the 'extern' keyword in C mean?


A) It speci es that the variable is de ned elsewhere.
B) It speci es that the variable is a constant.
C) It speci es that the variable is local.
D) It speci es that the variable is static.

46. Which of the following is true about the do-while loop?


A) It executes the loop body at least once.
B) It never executes the loop body.
C) It is the same as the while loop.
D) It does not check the loop condition.

47. Which of the following is a valid declaration of a pointer to an integer in C?


A) `int *ptr;`
B) `int ptr*;`
C) `int &ptr;`
D) `int ptr&;`

48. How do you comment a single line in C?


A) `// This is a comment`
B) `/* This is a comment */`
C) `# This is a comment`
D) `<!—This is a comment →`

49. Which of the following is not a valid storage class speci er in C?


A) auto
B) register
C) volatile
D) static

50. How do you declare an array of 10 integers in C?


A) `int arr[10];`
B) `int arr{10};`
C) `int arr = 10;`
D) `int arr;`

51. What is the disadvantage of arrays in C?


A) Multiple other data structures can be implemented using arrays
B) The amount of memory to be allocated needs to be known beforehand.
C) Elements of array can be accessed in constant time
D) Elements are stored in contiguous memory blocks

52. How many bytes does "int = D" use?


A) 1
B) 0
C) 10
D) 2 or 4

53. Directives are translated by the__________


A) Pre-processor
B) Compiler
C) Linker
fi
fi
fi
fi
fi
fi
D) Editor

54. What feature makes C++ so powerful?


A) Reusing the old code
B) Easy implementation
C) Easy memory management
D) All of the above

55. What is the maximum number of characters that can be held in the string variable char
address line[40]?
A) 39
B) 41
C) 40
D) 38

56. Which of the following STL template class is a container adaptor class?
A) Deque
B) Vector
C) List
D) Stack

57. If addition had higher precedence than multiplication, then the value of the expression (1 + 2 *
3 + 4 * 5) would be which of the following?
A) 27
B) 105
C) 69
D) 47

58. Each instance of a class has a di erent set of______


A) Attribute values
B) Class interfaces
C) Return types
D) Methods

59. What is the return type of the fopen() function in C?


A) An integer
B) Pointer to a FILE object
C) Pointer to an integer
D) None of the above

60. How to nd the length of an array in C?


A) sizeof(a)/sizeof(a[0])
B) sizeof(a)
C) sizeof(a)*sizeof(a[0])
D) None of the above

61. Which of the following is not a storage class speci er in C?


A) extern
B) volatile
C) typedef
D) static

62. Which of the following will occur if we call the free() function on a NULL pointer?
A) The program will execute normally
B) Compilation Error
C) Runtime error
D) Unde ned behavior

63. Which of the following should be used to free memory from a pointer allocated using the
"new" operator?
fi
fi
ff
fi
A) free()
B) realloc()
C) delete
D) None of the above

64. Which data structure is used to handle recursion in C?


A) Stack
B) Queue
C) Trees
D) Deque

65. Which of the following is not true about structs in C?


A) Functions are allowed inside structs
B) Constructors are not allowed inside structs
C) No data hiding
D) Cannot have static members in struct body

66. How is the 3rd element in an array accessed based on pointer notation?
A) *a + 3
B) *(*a+3)
C) *(a + 3)
D) & (a+3)

67. Which of the following function is used to open a le in C++?


A) fgets
B) fseek
C) fclose
D) fopen

68. Which of the following are correct le opening modes in C?


A) w
B) r
C) rb
D) All of the above

69. What is required in each C program?


A) The program does not require any function.
B) The program must have at least one function
C) Output data
D) Input data

70. In which of the following languages is function overloading not possible?


A) C++
B) C
C) Python
D) Java

71. What is the output of this statement "printf("%d", (a++))"?


A) Error message
B) The value of (a + 1)
C) The current value of a
D) Garbage

72. Why is a macro used in place of a function?


A) It reduces code size
B) It reduces execution time
C) It increases code size
D) It increases execution time

73. What is the 16-bit compiler allowable range for integer constants?
fi
fi
A) -3.4e38 to 3.4e38
B) -32767 to 32768
C) -32668 to 32667
D) -32768 to 32767

74. What is a lint?


A) Interactive debugger
B) C compiler
C) C interpreter
D) Analyzing tool

75. If p is an integer pointer with a value 1000, then what will the value of p + 5 be?
A) 1005
B) 1020
C) 1004
D) 1010

76. How to declare a double-pointer in C?


A) int **val
B) int *val
C) int **val
D) int &val

77. What is the size of the int data type (in bytes) in C?
A) 4
B) 8
C) 2
D) 1

78. In the C language, the constant is de ned _______.


A) Before main
B) Anywhere, but starting on a new line.
C) After main
D) None of the these

79. How are String represented in memory in C?


A) An Array of characters
B) Linked list of characters
C) The object of some class
D) Some as other primitive data types

80. Which of the following is an exit controlled loop?


A) While loop
B) For loop
C) do-while loop
D) None of the above

81. Consider the following program:


```c
struct node
{
int i;
oat j;
};
struct node *s[10];
```
The above C declaration de nes
A) An array, each element of which is pointer to a structure of type node
B) A structure of 2 elds, each eld being a pointer to an array of 10 elements
C) A structure of 3 elds: an integer, a oat, and an array of 10 elements
fl
fi
fi
fi
fi
fi
fl
D) An array, each element of which is a structure of type node

82. The number of tokens in


`printf("i = %d, &i – %x", i, &i);`
A) 3
B) 10
C) 25
D) 22

83. Assume that objects of the type short, oat and long occupy 2 bytes, 4 bytes and 8 bytes,
respectively. The memory requirement for variable t, ignoring alignment
```c
struct {
short s [5];
union {
oat y;
long z;
}u;
} t;
```
A) 22 bytes
B) 18 bytes
C) 14 bytes
D) 10 bytes

84. Consider the given three C functions:


```c
[P1] int * g (void)
{
int x = 10;
return (&x);
}
[P2] int * g (void)
{
int * px;
*px = 10;
return px;
}
[P3] int * g (void)
{
int * px
px = (int *) malloc (sizeof(int));
*px = 10;
return px;
}
```
Which of the above three functions are likely to cause problems?
A) Only P1 and P2
B) Only P3
C) Only P1 and P3
D) P1, P2, and P3

85. What does the given program print?


```c
char c[ ] = "GATE2011"
char *p = c;
printf ("%s", p + p[3] – p[1]);
```
A) GATE 2011
B) 2011
fl
fl
C) E2011
D) 011

86. The output of the following C program is__________


```c
void f1(int a, int b) {
int c;
c=a; a=b; b=c;
}
void f2(int *a, int *b) {
int c;
c=*a; *a=*b; *b=c;
}
int main(){
int a=4, b=5, c=6;
f1(a,b);
f2(&b, &c);
printf("%d",c-a-b);
}
```
A) -5
B) 6
C) -6
D) 0

87. The following program prints ___________


```c
#include <stdio.h>
void f (int *p, int *q) {
p = q;
*p = 2;
}
int i = 0, j = 1;
int main ( ){
f(&i, &j);
printf ("%d %d n", i, j);
return 0;
}
```
A) 2 2
B) 2 1
C) 0 1
D) 0 2

88. Consider the following C program


```c
void f(int, short);
void main()
{
int i = 100;
short s = 12;
short *p = &s;
__________ ; // call to f()
}
```
Which one of the following expressions, when placed in the blank above, will NOT result in a
type checking error?
A) f(s,*s)
B) i = f(i,s)
C) f(i,*s)
D) f(i,*p)

89. The output of the following C program is


```c
#include<stdio.h>
struct Ournode{
char x,y,z;
};
int main(){
struct Ournode p = {'1', '0', 'a'+2};
struct Ournode *q = &p;
printf ("%c, %c", *((char*)q+1), *((char*)q+2));
return 0;
}
```
A) 0, c
B) 0, a+2
C) '0', 'a+2'
D) '0','c'

90. Assume the following C variable declaration:


```c
int * A[10], B[10][10];
```
Of the following expressions
I. A[2]
II. A[2] [3]
III. B[1]
IV. B[2] [3]
Which will not give compile-time errors if used as left-hand sides of assignment statements in a
C program?
A) I, II and IV
B) II, III and IV
C) II and IV
D) IV only

91. In the C language:


a) At most one activation record exists between the current activation record and the activation
record for the main
b) The number of activation records between the current activation record and the activation
record for the main depends on the actual function calling sequence.
c) The visibility of global variables depends on the actual function calling sequence.
d) Recursion requires the activation record for the recursive function to be saved on a di erent
stack before the recursive function can be called.
A) There is no such restriction in C language
B) True
C) False. In C, variables are statically scoped, not dynamically
D) False. The activation records are stored on the same stack

92. Consider the following C program:


```c
#include <stdio.h>
int jumble(int x, int y){
x=2*x+y;
return x;
}
int main(){
int x=2, y=5;
y=jumble(y,x);
x=jumble(y,x);
ff
printf("%d n", x);
return 0;
}
```
The value printed by the program is _____
A) 26
B) 25
C) 20
D) 0

93. Consider the following C program:


```c
#include <stdio.h>
int main(){
int arr[]={1,2,3,4,5,6,7,8,9,0,1,2,5}, *ip=arr+4;
printf("%dn", ip[1]);
return 0;
}
```
The number that will be displayed on execution of the program is _______
A) 6
B) 7
C) 8
D) 0

94. Consider the following C function.


```c
void convert(int n){
if(n<0)
printf("%d",n);
else {
convert(n/2);
printf("%d",n%2);
}
}
```
Which one of the following will happen when the function convert is called with any positive
integer n as an argument?
A) It will print the binary representation of n and terminate
B) It will print the binary representation of n in the reverse order and terminate
C) It will print the binary representation of n but will not terminate
D) It will not print anything and will not terminate

95. Consider the following C program:


```c
#include <stdio.h>
int r(){
static int num=7;
return num–;
}
int main(){
for (r();r();r())
printf("%d",r());
return 0;
}
```
Which one of the following values will be displayed on execution of the programs?
A) 41
B) 52
C) 63
D) 630

96. Consider the following C program:


```c
#include <stdio.h>
int main(){
oat sum = 0.0, j = 1.0, i = 2.0;
while (i/j > 0.0625){
j = j + j;
sum = sum + i/j;
printf("%fn", sum);
}
return 0;
}
```
The number of times the variable sum will be printed, when the above program is executed, is
_________
A) 0
B) 5
C) 1
D) None of the above

97. Consider the following C program:


```c
#include <stdio.h>
int main()
{
int a[ ] = {2, 4, 6, 8, 10};
int i, sum = 0, *b = a + 4;
for (i = 0; i < 5; i++)
sum = sum + (*b – i) – *(b – i);
printf ("%dn", sum);
return 0;
}
```
The output of the above C program is ______
A) 10
B) 12
C) 15
D) 20

98. Consider the following C program:


```c
#include <stdio.h>
void fun1(char *s1, char *s2){
char *tmp;
tmp = s1;
s1 = s2;
s2 = tmp;
}
void fun2(char **s1, char **s2){
char *tmp;
tmp = *s1;
*s1 = *s2;
*s2 = tmp;
}
int main(){
char *str1 = "Hi", *str2 = "Bye";
fun1(str1, str2); printf("%s %s ", str1, str2);
fun2(&str1, &str2); printf("%s %s", str1, str2);
fl
return 0;
}
```
The output of the program above is
A) Hi Bye Bye Hi
B) Hi Bye Hi Bye
C) Bye Hi Hi Bye
D) Bye Hi Bye Hi

99. Consider the following C code. Assume that unsigned long int type length is 64 bits.
```c
unsigned long int fun(unsigned long int n){
unsigned long int i, j = 0, sum = 0;
for (i = n; i > 1; i = i/2) j++;
for ( ; j > 1; j = j/2) sum++;
return(sum);
}
```
The value returned when we call fun with the input 240 is
A) 4
B) 5
C) 6
D) 40

100. Consider the following C program:


```c
#include <stdio.h>
#include <string.h>
void printlength (char *s, char *t) {
unsigned int c=0;
int len = ((strlen(s) - strlen(t)) > c) ? strlen(s): strlen(t);
printf("%d\n",len);
}
void main () {
char *x = "abc",
char *y = "defgh";
printlength (x,y);
}
```
Recall that strlen is de ned in string.h as returning a value of type size_t, which is an unsigned
int.
The output of the program is _____
A) 3
B) 5
C) 7
D) 9
fi

You might also like