Test Bank - 1

You might also like

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

1. What is the output of the following C code snippet?

#include <stdio.h>

int main() {
printf("Hello, world!");
return 0;
}
A. Hello, world!
B. Hello
C. world
D. Compile-time error
Answer: A.
2. Which of the following is NOT a valid C identifier?
A. my_var
B. _myVar
C. 1_myvar
D. myVar1
Answer: C.
3. What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int x = 5;
printf("%d", x++);
printf("%d", x);
return 0;
}
A. 56
B. 55
C. 65
D. 6
Answer: A.
4. Which of the following is the correct syntax for declaring a
function that takes two integer parameters and returns an integer?
A. intmyFunction(int x, int y) { return x + y; }
B. function(int x, int y) int { return x + y; }
C. int function myFunction(int x, int y) { return x + y; }
D. int myFunction(int x int y) { return x + y; }
Answer: A.
5. What is the output of the following C code snippet?
#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
printf("%d", arr[2]);
return 0;
}
A. 1
B. 2
C. 3
D. 4
Answer: C.
6. Which of the following operators can be used to obtain the address
of a variable in C?
A. &
B. *
C. +
D. -
Answer: A.
7. What is the output of the following C code snippet?

#include <stdio.h>

int main() {
int x = 5, y = 7;
printf("%d", x > y ? x : y);
return 0;
}
A. 5
B. 7
C. 12
D. Compile-time error
Answer: B.
8. Which of the following statements about arrays in Cis FALSE?
A. An array is a collection of elements of the same data type.
B. The size of an array must be specified at the time of declaration.
C. The index of the first element of an array is always 0.
D. The elements of an array can be of different data types.
Answer: D.
9. What is the output of the following C code snippet?
arduino
#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%d", *(p + 3));
return 0;
}
A. 1
B. 2
C. 3
D. 4
Answer: D.
10.Which of the following is the correct syntax for declaring a pointer
to an integer in C?
A. int *p;
B. int p;
C. *int p;
D. pointer int p;
Answer: A.
11.What is the output of the following C code snippet?
ini

#include <stdio.h>

int main() {
int x = 5;
int *p = &x;
*p = 7;
printf("%d", x);
return 0;
}
A. 5
B. 7
C. Compile-time error
D. Run-time error
Answer: B.
12.Which of the following is the correct syntax for a while loop in C?
A. while (condition) { statement; }
B. while (statement) { condition; }
C. for (statement; condition; increment) { }
D. do { statement; } while (condition);
Answer: A.
13.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int x = 5;
int y = 7;
printf("%d", x & y);
return 0;
}
A. 0
B. 1
C. 5
D. 7
Answer: A.
14.Which of the following is the correct syntax for a switch
statement in C?
A.

switch (expression) {
case constant1:
statement1;
break;
case constant2:
statement2;
break;
default:
statement3;
}
B.

switch (expression)
case constant1:
statement1;
break;
case constant2:
statement2;
break;
default:
statement3;
C.
switch (expression) {
case constant1
statement1;
break;
case constant2
statement2;
break;
default
statement3;
}
D.
switch (expression)
case constant1:
statement1;
case constant2:
statement2;
default:
statement3;
}

Answer: A.

15. What is the output of the following C code snippet?

#include <stdio.h>
int main() {
int x = 5;
int *p = &x;
printf("%d", ++*p);
return 0;
}

A. 5
B. 6
C. Compile-time error
D. Run-time error

Answer: B.

16.What is the output of the following C code snippet?


#include <stdio.h>

int main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
return 0;
}
A. 1 2 3 4 5
B. 1 2 3 4
C. 1 2 3
D. 1
Answer: A.
17.Which of the following loop constructs is best suited for situations
where the loop body needs to be executed at least once?
A. for loop
B. while loop
C. do-while loop
D. if statement
Answer: C.
18.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
return 0;
}
A. 0 1 2 3 4
B. 1 2 3 4 5
C. 1 2 3 4
D.0 1 2 3 4
Answer: A.
19.What is the output of the following C code snippet?
arduino
#include <stdio.h>

int main() {
int i;
for (i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
printf("%d ", i);
}
return 0;
}
A. 1 3 5 7 9
B. 0 2 4 6 8
C. 1 2 3 4 5 6 7 8 9 10
D. None of the above
Answer: A.
20.Which of the following is the correct syntax for a nested for loop in
C?
A.

for (int i = 0; i < 10; i++) {


for (int j = 0; j < 5; j++) {
statement;
}
}
B.

for (int i = 0; i < 10; i++)


for (int j = 0; j < 5; j++)
statement;
C.

for (int i = 0; i < 10; i++) {


for (int j = 0; j < 5; j++)
statement;
}
D.

for (int i = 0; i < 10; i++)


{
for (int j = 0; j < 5; j++)
{
statement;
}
}
Answer: C.
21.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
return 0;
}
A. 0 1 2 3 4
B. 1 2 3 4 5
C. 1 2 3 4
D. 0 1 2 3 4 5
Answer: A.
22.Which of the following is the correct syntax for a nested while
loop in C?
A.

while (condition1) {
while (condition2) {
statement;
}
}
B.

while (condition1)
while (condition2)
statement;
C.

while (condition1) {
while (condition2)
statement;
}
D.
while (condition1)
{
while (condition2)
{
statement;
}
}
Answer: A.
23.What is the output of the followingC code snippet?
arduino

#include <stdio.h>

int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
return 0;
}
A. 0 1 2 3 4
B. 0 1 2 3 4 5
C. 1 2 3 4
D. 0 1 2 3 4 6 7 8 9
Answer: A.
24.Which of the following is the correct syntax for a nested do-while
loop in C?
A.

do {
do {
statement;
} while (condition2);
} while (condition1);
B.

do
do
statement;
while (condition2);
while (condition1);
C.

do {
do
statement;
while (condition2);
} while (condition1);
D.

do
{
do
{
statement;
}
while (condition2);
}
while (condition1);
Answer: C.
25.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int i;
for (i = 0; i < 5; ++i) {
for (int j = 0; j <= i; ++j) {
printf("*");
}
printf("\n");
}
return 0;
}
A.

*
**
***
****
*****
B.

*****
****
***
**
*
C.
asciidoc

*****
****
***
**
*
*
**
***
****
*****
D.
asciidoc

*
**
***
****
*****
******
*******
********
*********
**********
Answer: A.

26.What is the output of the following C code snippet?


arduino

#include <stdio.h>

int sum(int a, int b) {


return a + b;
}

int main() {
int result = sum(3, 4);
printf("%d", result);
return 0;
}
A. 3
B. 4
C. 7
D. Compile-time error
Answer: C.
27.Which of the following is the correct syntax for a function that
takes no parameters and returns no value in C?
A. void myFunction {}
B. void myFunction() {}
C. int myFunction() {}
D. myFunction() void {}
Answer: B.
28.What is the output of the following C code snippet?
ini

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x = 5, y = 7;
swap(&x, &y);
printf("%d %d", x, y);
return 0;
}
A. 5 7
B. 7 5
C. 5 5
D. 7 7
Answer: B.
29.Which of the following is the correct syntax for a function that
takes a pointer to an integer as a parameter and returns the value
pointed to by the pointer multiplied by 2?
A.

int myFunction(int *p) {


*p = *p * 2;
}
B.

int myFunction(int *p) {


return *p * 2;
}
C.

void myFunction(int *p) {


*p = *p * 2;
}
D.

int *myFunction(int p) {
return p * 2;
}
Answer: B.
30.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int square(int x) {
return x * x;
}

int main() {
int (*p)(int) = square;
printf("%d", p(5));
return 0;
}
A. 5
B. 25
C. Compile-time error
D. Run-time error
Answer: B.
32.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int x = 5;
int *p = &x;
printf("%d", *p);
return 0;
}
A. 5
B. &x
C. p
D. Compile-time error
Answer: A.
33.Which of the following is the correct syntax for declaring a pointer
to a float variable in C?
A. float *p;
B. float p;
C. p float;
D. *p float;
Answer: A.
34.What is the output of the following C code snippet?
ini

#include <stdio.h>

int main() {
int x = 5;
int *p = &x;
*p = 7;
printf("%d", x);
return 0;
}
A. 5
B. 7
C. &x
D. p
Answer: B.
35.Which of the following is the correct syntax for declaring a pointer
to a function that takes an integer parameter and returns a float
value in C?
A. float (*p)(int);
B. float *p(int);
C. float *p(int);
D. (*p)(float) int;
Answer: A.
36.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%d", *p + 3);
return 0;
}
A. 1
B. 4
C. 6
D. 7
Answer: D.
37.Which of the following is the correct syntax for declaring a pointer
to a function that takes no parameters and returns no value in C?
A. void (*p)();
B. void p();
C. (*p)();
D. () void *p;
Answer: A.
38.What is the output of the following C code snippet?
apache

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *p = &arr[2];
printf("%d", *(p - 1));
return 0;
}
A. 1
B. 2
C. 3
D. 4
Answer: B.
39.Which of the following is the correct syntax for declaring a
pointerto a character array in C?
A. char *p;
B. char p[];
C. char []p;
D. p char*;
Answer: A.
40.What is the output of the following C code snippet?
apache

#include <stdio.h>

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

41.What is the output of the following C code snippet?


ini

#include <stdio.h>

int main() {
int arr[5];
arr[0] = 1;
arr[1] = 2;
printf("%d", arr[0] + arr[1]);
return 0;
}
A. 1
B. 2
C. 3
D. 4
Answer: C.
42.Which of the following is the correct syntax for initializing a
character array in C?
A. char arr[] = "Hello";
B. char arr[6] = "Hello";
C. char arr[] = {'H', 'e', 'l', 'l', 'o'};
D. char arr[6] = {'H', 'e', 'l', 'l', 'o'};
Answer: A.
43.What is the output of the following C code snippet?
apache

#include <stdio.h>

int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[3]);
return 0;
}
A. 1
B. 4
C. 5
D. Compile-time error
Answer: B.
44.Which of the following is the correct syntax for declaring a two-
dimensional integer array with 3 rows and 4 columns in C?
A. int arr[3, 4];
B. int arr[3][4];
C. int arr[4][3];
D. int arr[3, 4][];
Answer: B.
45.What is the output of the following C code snippet?
apache

#include <stdio.h>

int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("%d", arr[1][2]);
return 0;
}
A. 1
B. 2
C. 6
D. 8
Answer: C.
46.Which of the following is the correct syntax for initializing a two-
dimensional character array with 2 rows and 5 columns in C?
A. char arr[2, 5] = {"Hello", "World"};
B. char arr[2][5] = {"Hello", "World"};
C. char arr[5][2] = {"Hello", "World"};
D. `char arr[2, 5][] = {"Hello",Answer: B.
47.What is the output of the following C code snippet?
apache

#include <stdio.h>

int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("%d", *(*(arr + 2) + 1));
return 0;
}
A. 2
B. 5
C. 8
D. 9
Answer: C.
48.Which of the following is the correct syntax for passing a two-
dimensional integer array of size 3x4 to a function in C?
A. void myFunction(int arr[][]) {}
B. void myFunction(int arr[3][4]) {}
C. void myFunction(int **arr) {}
D. void myFunction(int *arr[]) {}
Answer: B.
49.What is the output of the following C code snippet?
apache

#include <stdio.h>

int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("%d", *(*(arr + 1) + 2));
return 0;
}
A. 2
B. 5
C. 6
D. 8
Answer: C.

50.Which of the following is the correct syntax for defining a struct in


C?
A.

struct Person {
char name[];
int age;
};
B.

struct Person {
char name[20];
int age;
};
C.

struct {
char name[20];
int age;
} Person;
D.

struct {
char name[];
int age;
} Person;
Answer: B.
51.What is the output of the following C code snippet?
arduino

#include <stdio.h>

struct Person {
char name[20];
int age;
};

int main() {
struct Person p = {"John", 30};
printf("%s %d", p.name, p.age);
return 0;
}
A. John 30
B. p.name p.age
C. &p.name &p.age
D. Compile-time error
Answer: A.
52.Which of the following is the correct syntax for declaring a pointer
to a struct in C?
A. struct *p;
B. struct Person *p;
C. Person *p;
D. *Person p;
Answer: B.
53.What is the output of the following C code snippet?
arduino

#include <stdio.h>

struct Person {
char name[20];
int age;
};

int main() {
struct Person p = {"John", 30};
struct Person *ptr = &p;
printf("%s %d", ptr->name, ptr->age);
return 0;
}
A. John 30
B. ptr->name ptr->age
C. &ptr->name &ptr->age
D. Compile-time error
Answer: A.
54.Which of the following is the correct syntax for initializing a struct
in C?
A.
struct Person p;
p.name = "John";
p.age = 30;
B.

struct Person p = {"John", 30};


C.

struct Person {
char name[20];
int age;
} p = {"John", 30};
D.

Person p = {"John", 30};


Answer: B.

55.What is the output of the following C code snippet?

#include <stdio.h>

struct Point {
int x;
int y;
};

int main() {
struct Point p = {3, 4};
struct Point *ptr = &p;
printf("%d %d", (*ptr).x, (*ptr).y);
return
A. 3 4
B. ptr.x ptr.y
C. &ptr.x &ptr.y
D. Compile-time error

Answer: A.

56. Which of the following is the correct syntax for declaring a struct insi
de another struct in C?

A.
struct Person {
char name[20];
struct Address {
char street[20];
char city[20];
};
};

B.
struct Person {
char name[20];
struct {
char street[20];
char city[20];
} address;
};

C.
struct {
char name[20];
struct {
char street[20];
char city[20];
} address;
} Person;

D.
struct Person {
char name[20];
struct Address {
char street[20];
char city[20];
} address;
};

Answer: B.

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


#include <stdio.h>
int main()
{
int a = 5;
printf("%d %d %d", a++, ++a, a--);
return 0;
}
A. 6 6 5
B. 6 7 6
C. 6 7 5
D. 5 6 5
Answer: C. The order of evaluation of the function arguments is not
guaranteed, so the behavior of the above code is undefined. However, in
practice, the output of the printf statement will depend on the order in
which the expressions a++, ++a, and a-- are evaluated. In this case, the
first expression a++ is evaluated first, which increments the value of a to
6 but returns the original value of 5. Then, the second expression ++a is
evaluated, which increments the value of a to 7 and returns the new value
of 7. Finally, the third expression a-- is evaluated, which decrements the
value of a to 6 but returns the original value of 7. Therefore, the output of
the printf statement is 6 7 5.

2. Which of the following is thecorrect way to declare a constant


pointer to a constant integer in C?
A. const int *ptr;
B. int *const ptr;
C. const int *const ptr;
D. int const *const ptr;
Answer: C. Option C is the correct way to declare a constant pointer to a
constant integer in C. The const keyword before the pointer specifies that
the pointer itself is constant and cannot be reassigned to point to a
different memory location. The const keyword before the integer
specifies that the value of the integer is constant and cannot be modified
through the pointer. Option A declares a pointer to a constant integer,
which means the value of the integer cannot be modified through the
pointer, but the pointer itself can be reassigned. Option B declares a
constant pointer to a non-constant integer, which means the pointer
cannot be reassigned but the value of the integer can be modified through
the pointer. Option D is equivalent to option C in terms of type
declaration, but the order of the const and int keywords is reversed.
3. What is the output of the following code snippet?
apache

#include <stdio.h>
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr+3;
printf("%d", *(ptr--));
return 0;
}
A. 3
B. 4
C. 5
D. undefined behavior
Answer: B. The variable ptr is initialized to point to the fourth element of
the array arr (i.e., the element with value 4). The expression *(ptr--) first
dereferences ptr to get the value at the memory location it points to,
which is 4. Then, it decrements ptr to point to the previous element in the
array (i.e., the element with value 3). Therefore, the output of the printf
statement is 4.

4. Which of the following is not a valid way to declare a string in C?


A. char str[20] = "hello";
B. char *str = "world";
C. char str[] = {'h', 'e', 'l', 'l', 'o', '\0'};
D. char str[5] = {'w', 'o', 'r', 'l', 'd'};
Answer: D. Option D is not a valid way to declare a string in C because
the array size is not large enough to accommodate the null terminator '\0',
which is added automatically to the end of a string in C. Therefore, option
D declares an array of characters that is not a valid string. Option A
declares a character array of size 20 that is initialized with the string
"hello". Option B declares a pointer toa string literal that points to the
string "world". Option C declares a character array with an unspecified
size that is initialized with the characters of the string "hello" and the null
terminator '\0'.

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


apache

#include <stdio.h>
int main()
{
int a[] = {1, 2, 3, 4, 5};
int *ptr = (int*)(&a+1);
printf("%d %d", *(a+1), *(ptr-1));
return 0;
}
A. 2 5
B. 5 2
C. 2 4
D. undefined behavior
Answer: B. The expression &a gives the address of the entire array a,
which is a pointer to an array of 5 integers. Adding 1 to this pointer
increments it by the size of the array, which is 5 * sizeof(int). Therefore,
the variable ptr is initialized to point to the memory location just after the
end of the array a. The expression *(a+1) retrieves the value of the second
element of the array a, which is 2, and the expression *(ptr-1) retrieves
the value of the last element of the array a, which is 5. Therefore, the
output of the printf statement is 2 5.

6. Which of thefollowing is not a valid integer type in C?


A. short
B. long
C. unsigned short
D. float
Answer: D. Option D is not a valid integer type in C. It is a floating-point
type that represents real numbers with a fractional part. Options A, B, and
C are valid integer types in C. short and long are signed integer types that
occupy 2 and 4 bytes of memory, respectively, and unsigned short is
an unsigned integer type that occupies 2 bytes of memory.

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


arduino

#include <stdio.h>
int main()
{
int x = 5;
int y = x++ * 2 + ++x * 3;
printf("%d %d", x, y);
return 0;
}
A. 7 29
B. 6 29
C. 7 33
D. 6 33
Answer: A. The expression x++ first assigns the value of x to y, which is
5, and then increments the value of x to 6. The expression ++x then
increments the value of x to 7 and multiplies it by 3. Finally, the
expression x++ * 2 multiplies the original value of x (i.e., 6) by 2.
Therefore, the value of y is (5 * 2) + (7 * 3) = 29, and the value of x is 7.
Thus, the output of the printf statement is 7 29.

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


arduino

#include <stdio.h>
int main()
{
int i;
for(i=0; i<5; i++)
{
if(i == 2)
break;
printf("%d ", i);
}
return 0;
}
A. 0 1
B. 0 1 2
C. 0 1 3 4
D. 2 4
Answer: A. The for loop iterates from i=0 to i=4. The if statement inside
the loop checks if the value of i is equal to 2. If it is, the break
statement is executed, which terminates the loop. Therefore, the loop only
executes for the values of i less than 2. The printf statement inside the
loop prints the value of i for each iteration. Therefore, the output of the
printf statement is 0 1.

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


ebnf
#include <stdio.h>
int main()
{
int i, j;
for(i=0; i=3; i++)
{
for(j=i+1; j<=4; j++)
{
printf("(%d,%d) ", i, j);
}
}
return 0;
}
A. (0,1) (0,2) (0,3) (0,4) (1,2) (1,3) (1,4) (2,3) (2,4)
B. (0,1) (1,2) (2,3) (3,4)
C. (0,2) (0,3) (0,4) (1,3) (1,4) (2,4)
D. (1,0) (2,0) (3,0) (4,0) (2,1) (3,1) (4,1) (3,2) (4,2)
Answer: A. The outer for loop iterates from i=0 to i=3, and the inner for
loop iterates from j=i+1 to j=4.

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

#include <stdio.h>

int main() {
int x = 5;
printf("%d\n", x++ + ++x);
return 0;
}
A. 10
B. 11
C. 12
D. 13
Answer: C. The value of x++ is 5 because the post-increment operator
returns the value of x before incrementing it. The value of ++x is 7
because the pre-increment operator first increments x to 6 and then
returns the new value. Therefore, the expression x++ + ++x evaluates to 5
+ 7, which is equal to 12.
11.Which of the following is NOT a valid C variable name?
A. my_variable
B. _myVariable
C. MyVariable
D. 2myVariable
Answer: D. Variable names in C must start with a letter or an underscore,
and they can only contain letters, digits, and underscores.
Therefore, 2myVariable is not a valid variable name.
12.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int i = 0;
while (i < 10) {
if (i % 2 == 0) {
printf("%d ", i);
}
i++;
}
return 0;
}
A. 0 2 4 6 8
B. 1 3 5 7 9
C. 0 1 2 3 4 5 6 7 8 9
D. Nothing will be printed
Answer: A. The code snippet uses a while loop to iterate through the
variable i from 0 to 9. Inside the loop, the if statement checks whether i is
an even number by checking if the remainder of i divided by 2 is equal to
0. If i is even, it is printed to the console using the printf function.
Therefore, the output of the code will be 0 2 4 6 8.
13.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%d\n", *++p);
printf("%d\n", *p++);
printf("%d\n", *p);
return 0;
}
A. 2 2 3
B. 2 3 4
C. 3 3 4
D. 2 3 5
Answer: B. The code snippet creates an integer array arr with five
elements and initializes it with the values 1, 2, 3, 4, and 5. The pointer
variable p is initialized to point to the first element of the array. The
first printf statement dereferences the pointer ++p, which increments the
pointer to point to the second element of the array and then dereferences
it to print the value 2. The second printf statement dereferences the
pointer p++, which dereferences p to print the value 2 and then
increments p to point to the third element of the array. The
third printf statement dereferences the pointer p to print the value 3.
Therefore, the output of the code will be 2 3 4.
14.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int x = 5;
int y = 2;
printf("%d\n", x / y);
printf("%f\n", (float)x / y);
return 0;
}
A. 2 2.5
B. 2 2
C. 2.5 2
D. 2.5 2.5
Answer: B. The first printf statement divides the integer variable x by
the integer variable y, which results in integer division and gives the
value 2. The second printf statement casts x to a float before dividing it
by y, which results in floating-point division and gives the value 2.5.
However, the format specifier %f is used instead of %d to print the value,
which causes undefined behavior and may print garbage values or crash
the program. Therefore, the output of the code will be 2 2.

15.What is the output of the following C code snippet?


ini
#include <stdio.h>

int main() {
int x = 5;
int y = 10;
int *p1 = &x;
int *p2 = &y;
*p1 = *p2;
*p2 = 15;
printf("%d %d", x, y);
return 0;
}
A. 5 10
B. 10 15
C. 15 10
D. 15 15
Answer: C. The code snippet creates two integer variables x and y and
initializes them with the values 5 and 10, respectively. Two pointer
variables p1 and p2 are also declared and initialized to point to
the memory addresses of x and y, respectively. The values
that p1 and p2 point to are then swapped by dereferencing them and
assigning their values to each other. This means that x is now equal to y,
which is 10, and y is set to 15. Therefore, the output of the code will
be 15 10.
16.Which of the following is the correct way to declare a two-
dimensional array in C with three rows and four columns?
A. int arr[3, 4];
B. int arr[3][4];
C. int arr[4, 3];
D. int arr[4][3];
Answer: B. A two-dimensional array in C is declared using two sets
of square brackets, with the first set specifying the number of rows and
the second set specifying the number of columns. Therefore, the correct
way to declare a two-dimensional array in C with three rows and four
columns is int arr[3][4];.
17.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *p = &arr[2];
printf("%d\n", *--p);
printf("%d\n", *p++);
printf("%d\n", *p);
return 0;
}
A. 1 2 4
B. 1 3 4
C. 2 3 4
D. 2 2 4
Answer: B. The code snippet creates an integer array arr with five
elements and initializes it with the values 1, 2, 3, 4, and 5. The pointer
variable p is initialized to point tothe third element of the array, which has
the value 3. The first printf statement decrements the pointer --p, which
moves it to point to the second element of the array and then dereferences
it to print the value 2. The second printf statement dereferences the
pointer p++, which dereferences p to print the value 3 and then
increments p to point to the fourth element of the array. The
third printf statement dereferences the pointer p to print the value 4.
Therefore, the output of the code will be 1 3 4.
18.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
printf("%d\n", *arr);
printf("%d\n", *(arr + 2));
printf("%d\n", *(arr + 4));
return 0;
}
A. 1 3 5
B. 1 2 3
C. 1 3 4
D. 1 4 5
Answer: A. The code snippet creates an integer array arr with five
elements and initializes it with the values 1, 2, 3, 4, and 5. The
first printf statement dereferences the pointerarr, which points to the first
element of the array and prints the value 1. The second printf statement
adds the integer value 2 to the pointer arr using pointer arithmetic, which
makes it point to the third element of the array and then dereferences it to
print the value 3. The third printf statement adds the integer value 4 to the
pointer arr, which makes it point to the fifth element of the array and then
dereferences it to print the value 5. Therefore, the output of the code will
be 1 3 5.
19.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int i = 1;
while (i <= 10) {
if (i % 2 == 0) {
printf("%d ", i);
}
i++;
}
return 0;
}
A. 2 4 6 8 10
B. 1 3 5 7 9
C. 1 2 3 4 5 6 7 8 9 10
D. Nothing will be printed
Answer: A. The code snippet uses a while loop to iterate through the
variable i from 1 to 10. Inside the loop, the if statement checks
whether i is an even number by checking if the remainder of i divided by
2 is equal to 0. If i is even, it is printed to the console using
the printf function. Therefore, the output of the code will be 2 4 6 8 10.

20.What is the output of the following C code snippet?


ini

#include <stdio.h>

int main() {
int x = 10;
int *p = &x;
printf("%d\n", *p);
*p = 20;
printf("%d\n", x);
return 0;
}
A. 10 10
B. 10 20
C. 20 10
D. 20 20
Answer: B. The code snippet creates an integer variable x and initializes
it with the value 10. The pointer variable p is initialized to point to
the memory address of x. The first printf statement dereferences the
pointer p, which prints the value of x, which is 10. The second line
modifies the value of x via the pointer p by assigning the value 20 to the
memory location pointed to by p. The third printf statement prints the
new value of x, which is 20. Therefore, the output of the code will be 10
20.
21.Which of the following is NOT a valid way to open a file in C?
A. fopen("file.txt", "r")
B. fopen("file.txt","wx")
C. fopen("file.txt", "a")
D. fopen("file.txt", "rw")
Answer: D. The option "rw" is not a valid file mode in C. The correct file
modes in C are:
• "r" for reading
• "w" for writing (creates a new file or overwrites an existing file)
• "a" for appending (opens an existing file for writing at the end of
the file, or creates a new file if it doesn't exist)
• "x" for exclusive creation (creates a new file, but fails if the file
already exists)
• "b" for binary mode (used with "r", "w", "a", or "x" for binary files)
Therefore, the correct way to open a file in C to read its contents
is fopen("file.txt", "r"), to create a new file for writing without
overwriting an existing file is fopen("file.txt", "wx"), and to open an
existing file for appending is fopen("file.txt", "a").
22.What is the output of the following C code snippet?
apache

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *p = arr + 3;
printf("%d\n", *(p - 2));
printf("%d\n", *(p + 2));
return 0;
}
A. 2 5
B. 3 5
C. 2 4
D. 3 4
Answer: C. The code snippet creates an integer array arr with five
elements and initializes it with the values 1, 2, 3, 4, and 5. The pointer
variable p is initialized to point to the fourth element of the array, which
has the value 4. The first printf statement subtracts 2 from the
pointer p using pointer arithmetic, which makes it point to the second
element of the array and then dereferences it to print the value 2. The
second printf statement adds 2 to the pointer p, which makes it point to
the sixth element of the array (which is outside the bounds of the array)
and then dereferences it to print a garbage value. Therefore, the output of
the code will be 2 4.
23.What is the output of the following C code snippet?
arduino

#include <stdio.h>

int main() {
int x = 10;
if (x == 5 || x == 10) {
printf("x is either 5 or 10");
} else {
printf("x is neither 5 nor 10");
}
return 0;
}
A. xis either 5 or 10
B. x is neither 5 nor 10
C. x is greater than 5 and less than 10
D. x is less than 5 or greater than 10
Answer: A. The code snippet checks whether the value of integer
variable x is equal to either 5 or 10 using the logical OR operator ||.
Since x is equal to 10, the first condition is true, and the code inside the if
statement is executed, which prints the message "x is either 5 or 10".
Therefore, the output of the code will be x is either 5 or 10.

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


arduino

#include <stdio.h>
int main() {
int x = 5;
int y = ++x;
printf("%d %d", x, y);
return 0;
}
A. 5 6
B. 6 5
C. 6 6
D. 5 5
Answer: C. The code snippet creates an integer variable x and initializes
it with the value 5. The ++ operator is used to increment the value
of x before assigning it to the variable y. Therefore, x is incremented to 6,
and the value of y is also set to 6. The printf function is used to print the
values of x and y to the console. Therefore, the output of the code will
be 6 6.
30.Which of the following is NOT a valid way to declare a variable in
C?
A. int x;
B. float y;
C. char z;
D. `double a = 3.14.;
Answer: D. The variable declaration double a = 3.14.; containsa syntax
error. The correct declaration should be double a = 3.14; without the extra
dot at the end. Therefore, option D is the correct answer.
31.What is the output of the following C code snippet?

#include <stdio.h>

int main() {
int i;
for (i = 0; i < 5; i++) {
printf("%d ", i);
}
return 0;
}
A. 0 1 2 3 4
B. 1 2 3 4 5
C. 0 1 2 3 4 5
D. 1 1 1 1 1
Answer: A. The code snippet creates an integer variable i and initializes it
to 0. The for loop is used to iterate from 0 to 4 (inclusive) using the
variable i. Inside the loop, the printf function is used to print the value
of i followed by a space. Therefore, the output of the code will be 0 1 2 3
4.
32.What is the output of the following C code snippet?
applescript

#include <stdio.h>

int main() {
int x = 10;
if (x > 5 && x < 15) {
printf("x is greater than 5 and less than 15");
A. x is greater than 5 and less than 15
B. x is either greater than or equal to 15 or less than or equal to 5
C. x is greater than or equal to 15
D. x is less than or equal to 5

Answer: A. The code snippet checks whether the value of integer variable
`x` is greater than 5 and less than 15 using the logical AND operator `&&
`. Since `x` is equal to 10, the condition is true, and the code inside the if
statement is executed, which prints the message "x is greater than 5 and le
ss than 15". Therefore, the output of the code will be `x is greater than 5 a
nd less than 15`.

33. What is the output of the following C code snippet?

#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
smali

A. 1 2 3 4 5
B. 5 4 3 2 1
C. 1 3 5 7 9
D. 0 1 2 3 4
Answer: A. The code snippet createsan integer array `arr` with five
elements and initializes it with the values 1, 2, 3, 4, and 5. The `for` loop
is used to iterate over the elements of the array using the index variable
`i`. Inside the loop, the `printf` function is used to print the value of the
`i`-th element of the array followed by a space. Therefore, the output of
the code will be `1 2 3 4 5`.

34.What is the output of the following C code snippet?


arduino

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%d\n", *p);
printf("%d\n", *(p + 2));
return 0;
}
A. 1 3
B. 1 2
C. 3 5
D. 2 4
Answer: A. The code snippet creates an integer array arr with five
elements and initializes it with the values 1, 2, 3, 4, and 5. The pointer
variable p is initialized to point to the first element of the array. The
first printf statement dereferences the pointer p, which prints the value of
the first element of the array, which is 1. The second printf statement
adds 2 to the pointer p using pointer arithmetic, which makes it point to
the third element of the array and then dereferences it to print the value 3.
Therefore, the output of the code will be 1 3.
35.Which of the following is NOT a valid way to declarea function in
C?
A. void myFunction()
B. int myFunction(int x, int y)
C. float myFunction(float x)
D. double myFunction(int x, float y, char z)
Answer: D. The function declaration double myFunction(int x, float y,
char z) is not valid because it is missing the return type. The correct
declaration should be double myFunction(int x, float y, char z);.
Therefore, option D is the correct answer.
36.What is the output of the following C code snippet?
ini

#include <stdio.h>

int main() {
int x = 3, y = 4;
int *p = &x, *q = &y;
*p = *p + *q;
*q = *p - *q;
*p = *p - *q;
printf("%d %d", x, y);
return 0;
}
A. 3 4
B. 4 3
C. 7 4
D. 4 7
Answer: B. The code snippet creates two integer variables x and y with
the values 3 and 4, respectively. Two integer pointer variables p and q are
created and initialized to point to the addresses of x and y, respectively.
The values pointed to byp and q are then swapped using arithmetic
operations. First, the value pointed to by p is updated to the sum of the
values pointed to by p and q. Then, the value pointed to by q is updated to
the difference between the values pointed to by p and q. Finally, the value
pointed to by p is updated to the difference between the new value
of p and the original value of q. Therefore, after these operations, x will
have the value 4 and y will have the value 3, and the printf function will
print 4 3 to the console. Therefore, the correct answer is B.
37.What is the output of the following C code snippet?
ini

#include <stdio.h>

int main() {
int x = 5;
int *p = &x;
int **q = &p;
printf("%d", **q);
return 0;
}
A. 5
B. 10
C. Garbage value
D. Compile-time error
Answer: A. The code snippet creates an integer variable x with the value
5. A pointer variable p is created and initialized to point to the address
of x. A pointer to pointer variable q is created and initialized to point to
the address of p. The printf function is used to print the value pointed to
by **q, which is equivalent to the value pointed to by p, which is the
value of x. Therefore, the output of the code will be 5.
38.What is the output of the following C code snippet?
apache

#include <stdio.h>

int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int *p = arr[0];
printf("%d %d %d", *(p + 1), *(p + 5), *(p + 8));
return 0;
}
A. 2 5 9
B. 1 4 7
C. 3 6 9
D. 9 6 3
Answer: A. The code snippet creates a two-dimensional integer
array arr with three rows and three columns, and initializes it with the
values 1 through 9. A pointer variable p is created and initialized to point
to the first element of the first row of the array, which is the value 1.
The printf function is used to print the values of the second element of the
first row, the second element of the second row, and the third element of
the third row, respectively. These values are accessed using pointer
arithmetic byadding the appropriate offsets to the pointer p. Therefore,
the output of the code will be 2 5 9.

39.What is the output of the following C code snippet?


arduino

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int i;
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
A. 1 2 3 4 5
B. 5 4 3 2 1
C. 1 3 5 7 9
D. 0 1 2 3 4
Answer: A. The code snippet creates an integer array arr with five
elements and initializes it with the values 1, 2, 3, 4, and 5.
The sizeof operator is used to determine the total number of bytes
occupied by the array, which is then divided by the size of an individual
element to determine the number of elements in the array. The for loop is
used to iterate over the elements of the array using the index variable i.
Inside the loop, the printf function is used to print the value of thei-th
element of the array followed by a space. Therefore, the output of the
code will be 1 2 3 4 5.
40.Which of the following is NOT a valid C data type?
A. short
B. long long
C. boolean
D. unsigned char
Answer: C. boolean is not a valid data type in C. Instead,
the stdbool.h header file should be included, and the bool data type can be
used, which is defined as either true or false. Therefore, option C is the
correct answer.
41.What is the output of the following C code snippet?
apache

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *p = &arr[2];
printf("%d", *(p-1));
return 0;
}
A. 1
B. 2
C. 3
D. 4
Answer: B. The code snippet creates an integer array arr with five
elements and initializes it with the values 1, 2, 3, 4, and 5. A pointer
variable p is created and initialized to point to the address of the third
element of the array, which is the value 3. The printf function is used
toprint the value pointed to by p-1, which is equivalent to the value
pointed to by the address of the second element of the array, which is the
value 2. Therefore, the output of the code will be 2.
42.What is the output of the following C code snippet?
ini

#include <stdio.h>

int main() {
int x = 1, y = 2;
int *p = &x, *q = &y;
p = q;
*q = 0;
printf("%d %d", *p, *q);
return 0;
}
A. 0 0
B. 1 0
C. 0 2
D. 1 2
Answer: A. The code snippet creates two integer variables x and y with
the values 1 and 2, respectively. Two integer pointer variables p and q are
created and initialized to point to the addresses of x and y, respectively.
The value of p is then updated to point to the address of y. The value
pointed to by q is then updated to 0. Finally, the printf function is used to
print the values pointed to by p and q, which are both 0. Therefore, the
output of the code will be 0 0.
43.What is theoutput of the following C code snippet?
perl

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%d ", ++*p);
printf("%d ", *p++);
printf("%d ", (*p)++);
printf("%d", *p);
return 0;
}
A. 2 2 3 4
B. 2 2 4 4
C. 2 3 3 4
D. 3 2 4 4
Answer: C. The code snippet creates an integer array arr with five
elements and initializes it with the values 1, 2, 3, 4, and 5. A pointer
variable p is created and initialized to point to the first element of the
array. The printf function is used to print the values of the
expressions ++*p, *p++, (*p)++, and *p, respectively.
• The first expression ++*p increments the value pointed to
by p (which is 1) before dereferencing it, so it evaluates to 2.
• The second expression *p++ dereferences p to get the value 2, and
then increments p to point to the next element ofthe array, which is
the value 3. Therefore, it evaluates to 2.
• The third expression (*p)++ dereferences p to get the value 3, and
then increments the value pointed to by p to 4. Therefore, it
evaluates to 3.
• The fourth expression *p dereferences p to get the value 4.
Therefore, the output of the code will be 2 2 3 4.

44.What is the output of the following C code snippet?

#include <stdio.h>

int main() {
int arr[5];
printf("%d", arr[4]);
return 0;
}
A. 0
B. Garbage value
C. Compile-time error
D. Run-time error
Answer: B. The code snippet creates an integer array arr with five
elements, but it does not initialize any of the elements. The printf function
is used to print the value of the fifth element of the array arr[4], which is
uninitialized and contains a garbage value. Therefore, the output of the
code will be a garbage value.
45.Which of the following statements about pointers in C is FALSE?
A. A pointer can be dereferenced to obtain the value of the object it
points to.
B. A pointer can be assigned to point to a different object.
C. A pointer can be dereferenced to obtain the address of the object it
points to.
D. A pointer can be used to access elements of an array.
Answer: C. A pointer cannot be dereferenced to obtain the address of the
object it points to. Instead, the address of the object can be obtained using
the address-of operator &. Therefore, option C is the false statement.
46.What is the output of the following C code snippet?
apache

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *p = arr + 2;
printf("%d", *(p - 1));
return 0;
}
A. 1
B. 2
C. 3
D. 4
Answer: B. The code snippet creates an integer array arr with five
elements and initializes it with the values 1, 2, 3, 4, and 5. A pointer
variable p is created and initialized to point to the address of the third
element of the array, which is the value 3. The printf function is used to
print the value pointed to by p-1, which is equivalent to the value pointed
to by the address of the second element of the array, which is the value 2.
Therefore, the output of the code will be 2.
47.What is the output of the following C code snippet?
n1ql

#include <stdio.h>

void myFunction(int *p) {


*p = 2 * (*p);
}

int main() {
int x = 5;
myFunction(&x);
printf("%d", x);
return 0;
}
``A. 2
B. 5
C. 10
D. Compile-time error

Answer: C. The code snippet defines a function `myFunction` that takes a


pointer to an integer as a parameter. Inside the function, the value pointed
to by the pointer is multiplied by 2. The `main` function creates an integer
variable `x` with the value 5 and passes its address to the `myFunction` fu
nction. The value of `x` is then printed using the `printf` function. Theref
ore, the output of the code will be `10`.

48. What is the output of the following C code snippet?

#include <stdio.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
printf("%s", str1 + 2);
printf("%s", str2 + 1);
return 0;
}
scheme

A. llo orld
B. lloWorld
C. loWorld
D. Compile-time error

Answer: A. The code snippet creates two character arrays `str1` and `str2`
containing the strings "Hello" and "World", respectively. The `printf`
function is used to print the substrings of `str1` starting from the third
character (`str1 + 2`), which is "llo", and of `str2` starting from the
second character (`str2 + 1`), which is "orld". Therefore, the output of the
code will be `llo orld`.

You might also like