NEW
Font size
S
M
L
XL
Worksheetsc programming basics
Total questions: 60
Worksheet time: 30mins
Name
Class
Date
1.
Size of data types: Which of the following is true for a typical 32-bit GCC C compiler?
a)
sizeof(int) == sizeof(char)
b)
sizeof(int) == sizeof(long)
c)
sizeof(double) < sizeof(float)
d)
sizeof(char) == 2
2.
Integer division output: int a=5,b=2; printf("%d", a/b);
a)
2.5
b)
2
c)
3
d)
Compilation error
3.
Modulus operator output: int x=17,y=4; printf("%d", x%y);
a)
1
b)
2
c)
3
d)
4
4.
Pre- and post-increment: int a=10; int b = a++ + ++a; printf("%d %d", a,b);
a)
11 21
b)
12 21
c)
12 22
d)
11 22
5.
Logical vs bitwise AND: int a=5,b=0; if (b && a++) printf("True"); else printf("%d", a);
a)
True
b)
5
c)
6
d)
Compilation error
6.
Character constant: char c = 'A' + 2; printf("%c", c);
a)
A
b)
B
c)
C
d)
D
7.
Array indexing: int arr[] = {10,20,30,40,50}; printf("%d", arr[3]);
a)
20
b)
30
c)
40
d)
50
8.
Array + pointer: int arr[] = {1,2,3,4}; int *p = arr; printf("%d", *(p+2));
a)
1
b)
2
c)
3
d)
4
9.
String length: char s[] = "C language"; printf("%zu", strlen(s));
a)
9
b)
10
c)
11
d)
12
10.
sizeof string literal: char s[] = "C"; printf("%zu", sizeof(s));
a)
1
b)
2
c)
4
d)
Depends on compiler
11.
While loop sum from 1 to 5: what is sum?
a)
10
b)
12
c)
15
d)
5
12.
For loop scope: for(i=0;i<3;i++); printf("%d", i);
a)
0
b)
2
c)
3
d)
Undefined
13.
Break statement: for(i=1;i<=5;i++){ if(i==3) break; } printf("%d", i);
a)
3
b)
4
c)
5
d)
2
14.
Continue statement prints numbers skipping 3: output?
a)
12345
b)
1245
c)
1345
d)
12 4 5
15.
Function prototype valid in C?
a)
int sum(a,b);
b)
int sum(int a,int b);
c)
sum(int,int);
d)
int sum int a, int b;
16.
Function call result: printf("%d", add(2,3)*add(1,1));
a)
5
b)
10
c)
6
d)
8
17.
Pass by value example: change(int x){ x=100; } main prints a after change(a);
a)
10
b)
100
c)
0
d)
Garbage
18.
Pointer basic: int x=5; int *p=&x; *p=10; printf("%d", x);
a)
5
b)
10
c)
Address of x
d)
Compilation error
19.
Pointer to pointer: prints **pp where pp=&p and p=&x (x=7)
a)
Address of p
b)
Address of x
c)
7
d)
Garbage
20.
Pointer arithmetic (sizeof int =4): p=arr; p++; printf("%d", *p); arr={10,20,30}
a)
10
b)
20
c)
30
d)
Undefined
21.
static local variable in function called 3 times prints?
a)
1 1 1
b)
1 2 3
c)
3 3 3
d)
0 1 2
22.
sizeof vs array length: int arr[]={1,2,3,4}; n=sizeof(arr)/sizeof(arr[0]); printf n
a)
2
b)
3
c)
4
d)
8
23.
Ternary operator max of 10 and 20
a)
10
b)
20
c)
30
d)
Undefined
24.
switch without break cases 2 and 3: prints?
a)
Two
b)
Three
c)
TwoThree
d)
TwoThreeDefault
25.
const variable modification attempt causes?
a)
Compiles and runs, outputs 20
b)
Compilation error
c)
Runtime error
d)
UB without warning
26.
typedef unsigned long int ULI; ULI x; does what?
a)
Creates a macro
b)
Declares a structure
c)
Creates an alias for unsigned long int
d)
Creates an alias for unsigned int
27.
Correct malloc for 5 ints?
a)
int *p = malloc(5);
b)
int *p = (int*)malloc(5 * sizeof(int));
c)
int p = malloc(5 * sizeof(int));
d)
int *p = malloc(sizeof(int));
28.
NULL pointer check after malloc: which valid?
a)
if (p == 0)
b)
if (!p)
c)
if (p == NULL)
d)
All of the above
29.
fopen mode "w" does what?
a)
Opens for reading only
b)
Reading and writing (append)
c)
Writing, truncating existing content
d)
Appending, creating if not exists
30.
File code prints what? fopen write fprintf("Hello"); printf("Done");
a)
Hello
b)
Contents of test.txt
c)
Done
d)
Nothing
31.
What is the output?
#include <stdio.h>
int main() {
int a = 1;
printf("%d %d %d", a, a++, ++a);
return 0;
}
a)
1 1 3
b)
Undefined behavior
c)
1 2 3
d)
1 3 3
32.
Which header defines NULL in C?
a)
<stdio.h>
b)
<stdlib.h>
c)
<stddef.h>
d)
All of the above
33.
What is printed?
#include <stdio.h>
int main() {
char *s = "hello";
s[0] = 'H';
printf("%s", s);
return 0;
}
a)
Hello
b)
Compile error
c)
Runtime error (undefined behavior)
d)
hello
34.
What is the output?
#include <stdio.h>
int main() {
int x = -5;
printf("%u", x);
return 0;
}
a)
-5
b)
Implementation-defined unsigned representation of -5
c)
Compile error
d)
0
35.
What is printed?
#include <stdio.h>
int main() {
int a = 5;
int *p = &a;
p = p + 2;
printf("%p", (void*)p);
return 0;
}
a)
Address of a + 2*sizeof(int) (valid pointer arithmetic)
b)
Undefined behavior when dereferencing only — but printing pointer value is defined
c)
Compiler error
d)
Null
36.
What does volatile tell the compiler?
a)
Variable is constant
b)
Variable may change in ways unknown to optimizer (avoid optimizing accesses)
c)
Variable stored in cache only
d)
Variable is thread-safe
37.
What is the output?
#include <stdio.h>
int main() {
int a = 0;
if (a = 5) printf("True");
else printf("False");
return 0;
}
a)
True
b)
False
c)
Compilation error
d)
Undefined behavior
38.
What is printed?
#include <stdio.h>
int main() {
int i = 0;
for (; i < 3; printf("%d", i++)) {}
return 0;
}
a)
12
b)
1122
c)
12012
d)
210
39.
Which of the following is the correct prototype for main as per C standard?
a)
int main()
b)
int main(void)
c)
int main(int argc, char *argv[])
d)
All of the above
40.
What is the output?
#include <stdio.h>
int main() {
unsigned int x = -1;
printf("%d", x);
return 0;
}
a)
-1
b)
Implementation-defined large positive number printed as signed — undefined behavior due to mismatched format
c)
4294967295 (on 32-bit)
d)
Compile error
41.
What does sizeof(char) return by standard?
a)
1 (in bytes)
b)
8
c)
Depends on compiler
d)
Number of bits in char
42.
What is printed?
#include <stdio.h>
int main() {
int a = 1, b = 2;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%d %d", a, b);
return 0;
}
a)
2 1
b)
1 2
c)
0 0
d)
Undefined
43.
Which function is used to allocate memory that initializes to zero?
a)
malloc
b)
calloc
c)
realloc
d)
alloc
44.
What is the output?
#include <stdio.h>
int main() {
char s[] = "abc";
printf("%c", *(s + 3));
return 0;
}
a)
\0 (prints nothing)
b)
c
c)
a
d)
Undefined behavior
45.
Which of these is true about fgets()?
a)
It reads a line including newline into buffer and null-terminates it
b)
It discards newline always
c)
It returns the number of characters read as int
d)
It cannot read more than 10 characters
46.
What is the output?
#include <stdio.h>
void foo(int n) {
if (n == 0) return;
printf("%d", n);
foo(n-1);
}
int main() {
foo(3);
return 0;
}
a)
123
b)
321
c)
3 2 1
d)
111
47.
What is the output?
#include <stdio.h>
int main() {
int x = 5;
printf("%d %d", ++x, x++);
return 0;
}
a)
Undefined behavior
b)
6 6
c)
7 6
d)
6 7
48.
Which of these statements about enum in C is correct?
a)
enum constants have type int (or compatible integral type)
b)
enum variables cannot be printed using %d
c)
enum can't have specified values
d)
enum values are strings
49.
What is the output?
#include <stdio.h>
int main() {
int a = 10;
{
int a = 20;
printf("%d ", a);
}
printf("%d", a);
return 0;
}
a)
10 20
b)
20 10
c)
10 10
d)
20 20
50.
What will printf("%.*s", 3, "Hello") print?
a)
Hel
b)
Hello
c)
Compile error
d)
Undefined behavior
51.
What is the result of 5 >> 1 assuming arithmetic right shift and signed positive integer?
a)
2
b)
10
c)
0
d)
Implementation-defined
52.
What is printed?
#include <stdio.h>
int main() {
int a = 010; // leading zero
printf("%d", a);
return 0;
}
a)
10
b)
8
c)
0
d)
Compile error
53.
Which is true about realloc(ptr, 0)?
a)
Frees memory and returns NULL (implementation-defined)
b)
Same as free(ptr) and returns NULL always
c)
Allocates zero bytes and returns non-NULL pointer always
d)
Compile error
54.
What will the following print?
#include <stdio.h>
int main() {
int arr[3] = {1,2};
printf("%d", arr[2]);
return 0;
}
a)
0
b)
1
c)
Garbage value (but actually zero-initialized)
d)
Compile error
55.
Which conversion specifier prints size_t correctly?
a)
%d
b)
%u
c)
%zu
d)
%ld
56.
What is printed?
#include <stdio.h>
int main() {
int x = 1;
x += (x = 4);
printf("%d", x);
return 0;
}
a)
Undefined behavior
b)
5
c)
4
d)
1
57.
What is the complexity of strlen(s) for a C string of length n?
a)
O(1)
b)
O(log n)
c)
O(n)
d)
O(n^2)
58.
What is the output?
#include <stdio.h>
int main() {
char c = 300;
printf("%d", c);
return 0;
}
a)
44 (300 mod 256)
b)
Implementation-defined due to overflow of signed char
c)
Compile error
d)
300
59.
Which of the following statements about memcpy is true?
a)
Use memcpy for overlapping memory regions
b)
Use memmove for overlapping memory regions
c)
memcpy sets memory to zero
d)
memcpy returns number of bytes copied
60.
What will this code print?
#include <stdio.h>
int main() {
int a = 2147483647; // INT_MAX on 32-bit
a = a + 1;
printf("%d", a);
return 0;
}
a)
-2147483648 (undefined but common two's complement wrap)
b)
Undefined behavior by standard
c)
Compile error
d)
2147483648
Reset
