Font size
Worksheetsch456713
Total questions: 99
Worksheet time: 1hrs 14mins
What is an lvalue in C?
A) A label name
B) A literal constant
C) An object stored in computer memory
D) An error message
What does the statement count++ do?
Sets count to 0
Increments count by 1
Multiplies count by 2
Decreases count by 1
Which of the following is a correct use of assignment chaining?
i + j + k = 0;
i == j == k == 0;
i = j == k = 0;
i = j = k = 0;
What is the output of
i = 8; j = 4; i *= j++;
printf("%d %d", i, j); ?
32 4
40 5
40 4
32 5
Which form of increment returns the incremented value immediately?
x--
--x
++x
x++
In a switch statement, what data types can the controlling expression be?
int, char, enum
array
string and float
float and double
What is the output if grade = 3 ?
switch (grade) { case 4: printf("Excellent"); case 3: printf("Good"); case 2: printf("Average"); case 1: printf("Poor"); case 0: printf("Failing"); }
GoodAveragePoorFailing
Good
GoodAverage
GoodAveragePoor
Which Unix command is used to move or rename a file?
cd
mv
rm
cp
What does rm -i do?
Removes files with confirmation
Moves files
Copies files
Renames files
How can you clear the terminal screen?
Ctrl + l
rm screen
clear + l
Ctrl + c
Which relational operator has lower precedence than ?
==
=<
>
<
What is the output of
i=3; j=2; k=1;
printf("%d", i<=j==j);
A) 0
B) 1
C) true
D) false
Which operator is unary?
A) >=
B) ||
C) &&
D) !
Which of these tests if a value is between i and k?
i<j<k
i<j && j<k
i>j || j<k
i==j<k
Which of these if statements assigns 0 to i and skips the block?
if (i != 0)
if (i++)
if (i == 0)
if (i = 0)
Which keyword ends a case in a switch?
stop
return
break
end
What happens if a switch case is missing a break?
It stops the switch
The program exits the switch
A syntax error occurs
It continues into the next case
Which header file provides bool type in C99?
A) stdbool.h
B) stdio.h
C) stdlib.h
D) string.h
Which of these represents the correct form of if for multiple statements?
if (expression) { statements }
if expression (statements)
if expression { statements }
if (expression): statements
Which of these statements is true about scanf and format strings?
Scanf expects input to match non-whitespace characters in the format string.
Scanf ignores any input that doesn't match the format string.
Scanf uses default values for missing inputs.
Scanf always reads all input, even if it's incorrect.
What Unix text editor is guaranteed to be available on most systems?
nano
vi
code
emacs
Which command in vi saves and quits?
A) :q!
B) :w
C) :wq
D) :quit
In a while loop, when is the controlling expression evaluated?
After each iteration except the first
Only once at the start
After the loop body executes
Before the loop body executes
What happens when a while Loop controlling expression is always true?
Syntax error
The loop will execute only once
The loop won't execute
Infinite loop
Which statement is true about a do-while loop?
The expression is tested before the loop body executes
The expression must always be true
The loop body executes at least once
It cannot contain break statements
What is the output of this C code?
for (n = 9; n != 0; n--)
printf("%d ", n--);
9 7 5 3 3
9 7 5 3 1
9 8 7 6 5 4 3 2 1
8 6 4 2
Which operator is used to separate multiple expressions in a for loop?
&
,
:
;
What is the correct command to delete a line in vi?
p
yy
dd
u
Which C loop structure is best for counting iterations?
for
do-while
while
switch
Which keyword exits the innermost loop or switch?
continue
stop
break
return
Which keyword skips to the next iteration of the loop?
break
goto
pass
continue
What is the output of this C code?
sum=0; for(i=0;i<3;i++){
if(i%2==1) break; sum+=i; }
1
5
3
0
What is the output of this C code?
sum=0; for(i=0;i<3;i++){
if(i%2==1) continue; sum+=i; }
3
0
1
2
What is a null statement?
An empty statement marked by a semicolon
A statement with only a label
An incomplete declaration
A statement with variables but no operation
In which loop is this commonly seen: for (;;) ?
To loop a fixed number of times
To exit a loop immediately
To count up to 10
To create an infinite loop
What is the output of this code?
i=3; while(i>0); { printf("%d ", i); --i; }
A) 3 2 1 0
B) Does not compile
C) 3 2 1
D) Infinite loop
Which command pastes a copied line in vi?
yy
p
dd
rm
Which loop construct guarantees the loop body executes at least once?
do-while
for
switch
while
In vi, how do you undo the last action?
:undo
ctrl+z
:back
u
Which command in vi allows you to copy a line?
:wq
mv
yy
dd
What is the correct format specifier for printing an unsigned int?
%ld
%d
%i
%u
Which type conversion is safest to prevent data loss?
Converting float to int
Converting char to int
Converting double to float
Converting int to long
Which format specifier is used to print a long int?
%d
%ld
%f
%hd
Which of the following is an unsigned int?
short int
int
long int
unsigned int
What is integer overflow?
An arithmetic result is too large to represent
A variable is too large to store in memory
An error in scanf
The program crashes
What is the correct way to prevent overflow when multiplying two ints?
i = (long)(j * j);
i = j * (long)j;
i = (long) j * j;
i = j * j;
Which cast will convert an int to double in division?
(double)dividend / divisor
(float)dividend / divisor
(int)dividend / divisor
(long)dividend / divisor
Which floating type in C offers the highest precision?
extended
long double
float
double
How do you specify a float constant explicitly?
3.14F
F3.14
3.14
3.14L
What does the typedef typedef int Bool; do?
Changes bool's size
Makes a new integer type
Creates a type alias for int
Defines a macro
Which header defines types like size_t?
limits.h
typedefs in the C standard library
stdio.h
string.h
What is the ASCII value of 'A'?
97
48
32
65
Which of these reads a single character using scanf?
scanf("%c", &ch);
scanf("%s", &ch);
scanf("%f", &ch);
scanf("%d", &ch);
How does getchar() differ from scanf()?
It reads one character, including whitespace
It reads an entire line
It skips white space
It returns a char
Which loop skips characters until a newline using getchar()?
do { ch = getchar(); }
while (ch != '\n');
while (ch != ' ');
do { ch = getchar(); }
while (ch == '\n');
do { ch = getchar(); }
while (ch == ' ');
What is the purpose of toupper(ch)?
Tests if a character is alphabet
Converts character to lowercase
Converts character to uppercase
Converts a number to character
Which header must be included to use toupper()?
string.h
ctype.h
stdio.h
stdbool.h
Which format specifier is used to print a float value?
%ld
%f
%c
%d
Which of the following is a character constant?
'A'
"A"
A
char A
What is the output of: char ch = 'b'; ch = ch + 3; ?
'k'
'u'
'd'
'e'
What does int a[10]; do in C?
Declares an integer with value 10
Declares an array of 10 integers
Initializes 10 arrays with default values
Allocates memory dynamically for 10 variables
Why is #define N 10 often used when declaring arrays?
It makes code easier to debug
It allows easy updates to array size
It improves runtime performance
It automatically initializes the array
Which of these is a valid way to initialize an array?
array a = [1, 2, 3];
int a[] = {1, 2, 3};
int a[] = 10;
int a = {1, 2, 3};
What happens if you access an array index outside its declared size in C?
The program stops automatically
It results in undefined behavior
It wraps around to index 0
It gives a compile-time error
What is the index of the first element in a C array?
0
-1
Depends on the array type
1
What is stored in an array if it's only partially initialized?
Compile-time error
Random values
Previous values in memory
Remaining elements are set to 0
What type of loop is commonly used to traverse an array?
for loop
while loop
do-while loop
switch loop
What happens when you assign a value to a[5] in a 5-element array?
It causes undefined behavior
It gives a warning
It stores at the last element
It returns 0
What is the correct syntax to read values into an array?
scanf("%d", &a);
scanf(a[i]);
scanf("%d", &a[i]);
scanf("%d", a);
What is the maximum index in int b[100]; ?
98
99
100
101
How are array elements stored in memory in C?
Reversed
Randomly
Alternating
Contiguously
Which of the following is NOT allowed?
int a[] = {1,2};
x[2] = 5;
int x[10];
int b[0];
Can arrays be returned directly from a function in C?
Yes, as arrays
No, arrays are constants
Yes, using structs only
No, but you can return a pointer
Which operator is used to access array elements?
. (dot)
&
[] (brackets)
->
What does the following do? int a[2] = {5};
Sets a[0] = 5, a[1] = 0
Skips initialization
Initializes all to 5
Causes an error
How are 2D arrays declared in C?
array a(3,4);
int a[3,4];
int a = [3][4];
int a[3][4];
What is the row-major order in C?
Reverse storage
Bitwise mapping
Row-wise storage
Column-wise storage
Can a 2D array be partially initialized?
Yes, and remaining elements become 0
No
Only for first row
Yes, and rest stay uninitialized
How many elements are in int x[][3] = {{1,2,3},{4,5,6}};?
3
9
0
2 rows, 3 columns → 6 elements
What is the output of this code?
int a[] = {1, 2, 3};
printf("%d", a[1]);
Garbage value
0
2
1
What will sizeof(a) return in this code?
int a[5];
Assume int is 4 bytes.
8
10
4
20
How can you initialize all values of an integer array to zero?
int a[5] = {0};
int a = {0};
int a[] = {1,1,1};
int a[5] = 0;
What’s the correct way to loop through an array a[5]?
for(i=0;i<=5;i++)
for(i=0;i<5;i++)
for(i=1;i<5;i++)
for(i=1;i<=5;i++)
What is printed by this code?
int arr[] = {4, 5, 6};
printf("%d", arr[3]);
4
Garbage value or crash
5
6
Which of these is valid syntax to pass an array to a function?
void foo(int arr[]);
void foo(int arr[10]);
void foo(int* arr);
All of the above
Can you assign arrays directly in C like arr1 = arr2;?
Only for character arrays
Yes
Only for static arrays
No, must copy element-by-element
How do you declare a 2D array of 3 rows and 4 columns?
array[3,4];
int a[4][3];
int a[12];
int a[3][4];
How are arrays passed to functions?
By reference (as pointers)
By address copy
By value
Not allowed
What is printed by this?
int a[3] = {1, 2, 3};
int *p = a;
printf("%d", *(p + 2));
2
1
Error
3
Which function can copy arrays safely in C?
assign()
memcpy()
strcpy()
alloca()
What is the actual type of a string literal like "abc" when declared in C?
char[]
string
char *
int *
What type does a string literal like "abc" decay to when used in an expression or passed to a function in C?
char[]
char *
int *
string
How many bytes are set aside for the string "abc" in memory?
8
5
It depends on the compiler
4
What character is used to mark the end of a string in C?
\n
\0
\e
\t
Which of the following creates a null pointer to a string (i.e., not pointing to any string)?
char s[] = "";
char *s = NULL;
char *s = "\n";
char s[] = "NULL";
Which of the following correctly defines an empty string (a null string containing only '\0')?
char *s = NULL;
char s[] = "NULL";
char *s = "\n";
char s[] = "";
What will the following code do?
char *p = "abc";
*p = 'z';
Change 'a' to 'z'
Produce a warning
Cause undefined behavior
Compile but do nothing
What does this declaration define: char date1[8] = "June 15";
An incomplete array
A char pointer
A character array with a null terminator
A string literal
What size must a character array have to hold an 80-character string?
81
83
79
80
