wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Computer Programming-2

Total questions: 30

Worksheet time: 15mins

Name
Class
Date
1.

How many times will the following for loop iterate?

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

// code here

}

a)

5

b)

4

c)

3

d)

2

2.

What is the syntax of a simple for loop in C?

a)

for (initialize; condition; increment) { }

b)

for (initialize: condition: increment); { }

c)

for (initialize; condition: decrement); { }

d)

for (initialize: condition: decrement) { }

3.

What is the correct way to declare a 1-D array named "numbers" of 5 integers in C?

a)

int numbers[ ] = {5};

b)

numbers [5];

c)

numbers = {5};

d)

int numbers [5];

4.

What is the output of the following code snippet?

char str[5] = "Hello";

printf("%c", str[4]);

a)

\0

b)

e

c)

l

d)

o

5.

What is not the correct way to initialize an array named "grades" with the values 90, 85, 95, and 80 in C?

a)

int grade[10] ={90, 85, 95, 80};

b)

int grades[4] = {90, 85, 95, 80};

c)

int grades[ ] = {90, 85, 95, 80};

d)

int grades[4] = {90, 85, 95, 80}

6.

Which of the following functions can be used to find the length of a string in C?

a)

strlenn()

b)

strnlen()

c)

strlen()

d)

strlan()

7.

What is the output of the following code snippet?

char str1[10] = "Hello";

char str2[20] = {'H', 'e', 'l', 'l', 'o', ' ','\0'};

if (strcmp(str1, str2) == 0) {

printf("Equal");

} else {

printf("Not equal");

}

a)

Runtime Error

b)

Equal

c)

Compile time Error

d)

Not Equal

8.

What is the output of the following code snippet?

int arr[5] = {1, 2, 3, 4};

int i=2;

printf("%d", arr[i]);

a)

1,2,3,4

b)

compile-time error

c)

3

d)

2

9.

Following is the array declaration so what will be the value of name[4]?

char name[10] = "John";

char neme[10] = "Jhone";

a)

'\0'

b)

'e'

c)

'o'

d)

'h'

e)

'n'

10.

What is the output of the following code snippet?

char message[20] = "Hello";

printf("%c", message);

a)

H

b)

Compile-time Error

c)

Hello

d)

\0

11.

What is the output of the following code snippet?

int n[10] = {1,6,45,32,45};

printf("%d", n[5]);

a)

1

b)

compile-time error

c)

0

d)

45

12.

What is the output of the following code snippet?

char name[100] = "leanth of snippet ";

printf("%d", strlen(name));

a)

18

b)

100

c)

run-time error

d)

compile-time error

e)

15

13.

What is the output of the following code snippet?

int factorial(int n) {

if (n == 0)

return 1;

else

return n * factorial(n - 1);

}

printf("%d", factorial(1));

a)

Error

b)

0

c)

120

d)

1

14.

What is the output of the following code snippet?

int addNumbers(int a, int b) {

return a + b;

}

int result = addNumbers(5, 3);

printf("%d", result);

a)

2

b)

3

c)

5

d)

8

15.

What is the output of the following code snippet?

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("The sum of %d and %d is %d", num1, num2, num1 + num2);

a)

The sum of num1 and num2 is num1 + num2

b)

The sum of num1 and num2 is <sum>

c)

Enter two numbers: <num1> <num2> The sum of <num1> and <num2> is <sum>

d)

Depends on the input

16.

What is the purpose of using a structure in C?

a)

To store multiple values of the same data type

b)

To store different values of different data types

c)

To perform mathematical calculations

d)

To control the flow of a program

17.

How do you access the members of a structure in C?

a)

By using the dot (.) operator

b)

By using the arrow (->) operator

c)

By using the asterisk (*) operator

d)

By using the comma (,) operator

18.

What is the output of the following code snippet?

struct Point {

int x;

int y;

};

struct Point p1 = {3, 4};

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

a)

(4)

b)

(3)

c)

(4,3)

d)

(3,4)

19.

What is the syntax for declaring a structure in C?

a)

struct sample { int a; int b; };

b)

struct { int a, b; }

c)

struct { int a; int b; }

d)

All

20.

Which of the following is the correct way to declare a variable of structure type "Person" named "p1"?

a)

Person::p1;

b)

struct Person p1;

c)

Person p1;

d)

struct p1;

21.

What is the output of the following code snippet?

char str[] = "Hello";

char *p = str;

while (*p != '\0') {

printf("%c", *p);

p++;

}

a)

olleH

b)

ello

c)

Hello

d)

Error

22.

What is the output of the following code snippet?

FILE *file = fopen("data.txt", "r");

if (file == NULL) {

printf("File could not be opened.");

} else {

printf("File opened successfully.");

fclose(file);

}

a)

File could not be opend.

b)

File opend successfully.

c)

Error

d)

Depends on compiler

23.

Which of the following is the correct way to close a file in C?

a)

close;

b)

close(file);

c)

exit;

d)

fclose(file);

24.

Which of the following is the correct way to open a file named "data.txt" in C for append mode?

a)

fopen("data.txt", "+a");

b)

fopen("data.txt", "r+");

c)

fopen("data.txt", "a");

d)

fopen("data.txt", "a+");

25.

What is the output of the following code snippet?

int i;

for (i = 0; i < 5; i++) {

printf("%d ", i);

}

a)

12345

b)

0123

c)

012345

d)

01234

26.

What is the output of the following code snippet?

int i = 5;

while (i > 0) {

printf("%d ", i);

i--;

}

a)

01234

b)

43210

c)

12345

d)

54321

27.

What is the output of the following code snippet?

int i, j;

for (i = 1; i <= 3; i++) {

for (j = 1; j <= i; j++) {

printf("%d ", j);

}

}

a)

112123

b)

123123

c)

111223

d)

1212123

28.

What is the output of the following code snippet?

int i, j;

for (i = 0; i < 3; i++) {

for (j = 0; j < 2; j++) {

printf("%d%d ", i, j);

}

}

a)

00 10 20 01 11 21

b)

00 01 10 11 10 21

c)

01 02 03 04 05 06

d)

11 22 21 22 31 32

29.

What is the output of the following code snippet?

int i;

for (i = 0; i < 5; i++) {

if (i == 2)

continue;

printf("%d ", i);

}

a)

01234

b)

0124

c)

0134

d)

2

30.

What is the output of the following code snippet?

int i;

for (i = 0; i < 5; i++) {

if (i == 2)

break;

printf("%d ", i);

}

a)

012

b)

01234

c)

01

d)

2