NEW
Font size
WorksheetsComputer Programming-2
Total questions: 30
Worksheet time: 15mins
How many times will the following for loop iterate?
for (int i = 0; i < 5; i++) {
// code here
}
5
4
3
2
What is the syntax of a simple for loop in C?
for (initialize; condition; increment) { }
for (initialize: condition: increment); { }
for (initialize; condition: decrement); { }
for (initialize: condition: decrement) { }
What is the correct way to declare a 1-D array named "numbers" of 5 integers in C?
int numbers[ ] = {5};
numbers [5];
numbers = {5};
int numbers [5];
What is the output of the following code snippet?
char str[5] = "Hello";
printf("%c", str[4]);
\0
e
l
o
What is not the correct way to initialize an array named "grades" with the values 90, 85, 95, and 80 in C?
int grade[10] ={90, 85, 95, 80};
int grades[4] = {90, 85, 95, 80};
int grades[ ] = {90, 85, 95, 80};
int grades[4] = {90, 85, 95, 80}
Which of the following functions can be used to find the length of a string in C?
strlenn()
strnlen()
strlen()
strlan()
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");
}
Runtime Error
Equal
Compile time Error
Not Equal
What is the output of the following code snippet?
int arr[5] = {1, 2, 3, 4};
int i=2;
printf("%d", arr[i]);
1,2,3,4
compile-time error
3
2
Following is the array declaration so what will be the value of name[4]?
char name[10] = "John";
char neme[10] = "Jhone";
'\0'
'e'
'o'
'h'
'n'
What is the output of the following code snippet?
char message[20] = "Hello";
printf("%c", message);
H
Compile-time Error
Hello
\0
What is the output of the following code snippet?
int n[10] = {1,6,45,32,45};
printf("%d", n[5]);
1
compile-time error
0
45
What is the output of the following code snippet?
char name[100] = "leanth of snippet ";
printf("%d", strlen(name));
18
100
run-time error
compile-time error
15
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));
Error
0
120
1
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);
2
3
5
8
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);
The sum of num1 and num2 is num1 + num2
The sum of num1 and num2 is <sum>
Enter two numbers: <num1> <num2> The sum of <num1> and <num2> is <sum>
Depends on the input
What is the purpose of using a structure in C?
To store multiple values of the same data type
To store different values of different data types
To perform mathematical calculations
To control the flow of a program
How do you access the members of a structure in C?
By using the dot (.) operator
By using the arrow (->) operator
By using the asterisk (*) operator
By using the comma (,) operator
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);
(4)
(3)
(4,3)
(3,4)
What is the syntax for declaring a structure in C?
struct sample { int a; int b; };
struct { int a, b; }
struct { int a; int b; }
All
Which of the following is the correct way to declare a variable of structure type "Person" named "p1"?
Person::p1;
struct Person p1;
Person p1;
struct p1;
What is the output of the following code snippet?
char str[] = "Hello";
char *p = str;
while (*p != '\0') {
printf("%c", *p);
p++;
}
olleH
ello
Hello
Error
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);
}
File could not be opend.
File opend successfully.
Error
Depends on compiler
Which of the following is the correct way to close a file in C?
close;
close(file);
exit;
fclose(file);
Which of the following is the correct way to open a file named "data.txt" in C for append mode?
fopen("data.txt", "+a");
fopen("data.txt", "r+");
fopen("data.txt", "a");
fopen("data.txt", "a+");
What is the output of the following code snippet?
int i;
for (i = 0; i < 5; i++) {
printf("%d ", i);
}
12345
0123
012345
01234
What is the output of the following code snippet?
int i = 5;
while (i > 0) {
printf("%d ", i);
i--;
}
01234
43210
12345
54321
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);
}
}
112123
123123
111223
1212123
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);
}
}
00 10 20 01 11 21
00 01 10 11 10 21
01 02 03 04 05 06
11 22 21 22 31 32
What is the output of the following code snippet?
int i;
for (i = 0; i < 5; i++) {
if (i == 2)
continue;
printf("%d ", i);
}
01234
0124
0134
2
What is the output of the following code snippet?
int i;
for (i = 0; i < 5; i++) {
if (i == 2)
break;
printf("%d ", i);
}
012
01234
01
2
