Unit 2: Principles of programming - Practice Quiz

CAP1008 — C Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which function is the mandatory entry point of every C program where execution begins?

C program structure Easy
A. begin()
B. printf()
C. start()
D. main()

2 Which section of a C program is used to include header files like #include <stdio.h>?

C program structure Easy
A. Preprocessor directive section
B. Return statement section
C. Function definition section
D. Local declaration section

3 Which of the following is NOT part of the C character set?

character set Easy
A. Special symbol @
B. Greek letter
C. Digit 5
D. White space (blank)

4 Which of the following is a valid identifier in C?

identifiers and keywords Easy
A. total_marks
B. float
C. 2ndvalue
D. my var

5 How many keywords are defined in standard (ANSI) C?

identifiers and keywords Easy
A. 48
B. 32
C. 16
D. 24

6 Which keyword is used to declare a constant whose value cannot be changed during program execution?

constants and variables Easy
A. final
B. fixed
C. const
D. static

7 What is a variable in C?

constants and variables Easy
A. A preprocessor command
B. A fixed value that never changes
C. A named memory location whose value can change during program execution
D. A reserved word of the language

8 Which data type is used to store a single character in C?

data types Easy
A. double
B. char
C. float
D. int

9 Which of the following is used to store numbers with a decimal point?

data types Easy
A. int
B. char
C. void
D. float

10 Which format specifier is used to print an integer using printf()?

unformatted and formatted i/o functions Easy
A. %s
B. %f
C. %c
D. %d

11 Which function is used to read a single character from the keyboard?

unformatted and formatted i/o functions Easy
A. puts()
B. putchar()
C. getchar()
D. printf()

12 Which function is used to display a string on the screen and automatically moves the cursor to a new line?

unformatted and formatted i/o functions Easy
A. getchar()
B. gets()
C. scanf()
D. puts()

13 In scanf("%d", &n);, what does the & symbol represent?

unformatted and formatted i/o functions Easy
A. Address of the variable n
B. A comment marker
C. Logical AND operation
D. Value stored in n

14 What is the result of the expression 5 + 3 * 2 in C?

expressions Easy
A. 11
B. 10
C. 16
D. 13

15 Which arithmetic operator returns the remainder of a division?

arithmetic operators Easy
A. /
B. %
C. *
D. -

16 What does the ++ operator do in C?

unary operator Easy
A. Increases the value of a variable by 1
B. Decreases the value of a variable by 1
C. Compares two values for equality
D. Adds two variables together

17 Which operator is used to check whether two values are equal in C?

relational operator Easy
A. !=
B. >=
C. ==
D. =

18 Which of the following is the logical AND operator in C?

logical operator Easy
A. &&
B. !
C. ||
D. &

19 What is the value of x after int x = (5 > 3) ? 10 : 20;?

assignment and conditional operator Easy
A. 20
B. 10
C. 3
D. 5

20 Which of the following is the bitwise OR operator in C?

bitwise operators Easy
A. ^
B. &
C. |
D. ||

21 In a standard C program, which section is used to include header files and define macros before the main() function?

C program structure Medium
A. Function definition section
B. Local declaration section
C. Global declaration section
D. Preprocessor directive section

22 Which of the following is NOT part of the valid C character set?

character set Medium
A. Digits 0 to 9
B. White space characters
C. @ used as an operator
D. Special characters like # and %

23 Which of the following is a valid C identifier?

identifiers and keywords Medium
A. my-var
B. 2ndValue
C. _total_2
D. float

24 How many keywords are reserved in standard ANSI C (C89/C90)?

identifiers and keywords Medium
A. 32
B. 24
C. 48
D. 40

25 What is the output of the following code?
c
const int x = 10;
x = 20;
printf("%d", x);

constants and variables Medium
A. 10
B. Compilation error
C. 0
D. 20

26 On a typical 32-bit system, what is the size (in bytes) of a double variable?

data types Medium
A. 4
B. 16
C. 8
D. 2

27 What is the output of the following code?
c
char c = 'A';
printf("%d", c);

data types Medium
A. 65
B. 0
C. 97
D. A

28 Which function reads a full line of text including spaces until a newline is encountered?

unformatted and formatted i/o functions- printf(), scanf(), puts(), gets(), getchar(), putchar() Medium
A. scanf("%s", str)
B. getchar()
C. putchar()
D. gets()

29 What does printf("%d", printf("AB")); output?

unformatted and formatted i/o functions- printf(), scanf(), puts(), gets(), getchar(), putchar() Medium
A. AB2
B. AB1
C. 2AB
D. AB0

30 In scanf, why is the & operator used before variable names for basic types like int?

unformatted and formatted i/o functions- printf(), scanf(), puts(), gets(), getchar(), putchar() Medium
A. To convert the value to hexadecimal
B. To pass the address so scanf can store the value
C. To read multiple values at once
D. To indicate the variable is a constant

31 What is the value of the expression 5 + 3 * 2 - 8 / 4?

expressions Medium
A. 12
B. 4
C. 6
D. 9

32 What is the output of the following code?
c
printf("%d", 17 % 5);

arithmetic operators Medium
A. 3
B. 2
C. 0
D. 3.4

33 What is the output of the following code?
c
int a = 5;
int b = a++ + ++a;
printf("%d", b);

unary operator Medium
A. 11
B. 10
C. 12
D. 13

34 What is the value of the expression (10 > 5) > 3 in C?

relational operator Medium
A. 0
B. 1
C. 3
D. Compilation error

35 In the expression a && b, if a evaluates to 0, what happens to b?

logical operator Medium
A. b is always evaluated first
B. b is evaluated and result is 1
C. b is not evaluated due to short-circuiting
D. A compilation error occurs

36 What is the value of max after this code runs?
c
int a = 8, b = 12;
int max = (a > b) ? a : b;

assignment and conditional operator Medium
A. 8
B. 12
C. 1
D. 0

37 After executing int x = 10; x += x -= 5;, what is the value of x?

assignment and conditional operator Medium
A. 5
B. 0
C. 15
D. 10

38 What is the result of 12 & 10 in C?

bitwise operators Medium
A. 14
B. 2
C. 8
D. 6

39 What is the value of 5 << 2?

bitwise operators Medium
A. 20
B. 7
C. 10
D. 25

40 What is the output of the following code?
c
printf("%d", (int)(7.0 / 2));

type conversion and type modifiers Medium
A. 4
B. 2
C. 3
D. 3.5

41 Assuming 32-bit int, what is the value of the expression ~5 & 0xFF when printed with %d?

bitwise operators Hard
A. -6
B. 5
C. 251
D. 250

42 In C, what is the result of -7 % 3 and 7 % -3 respectively (C99 and later)?

arithmetic operators Hard
A. 1 and 1
B. 2 and -2
C. -1 and 1
D. -1 and -1

43 Given int i = 5;, what value is assigned in int j = i++ + ++i; and what is i afterward? (Common compiler behavior)

unary operator Hard
A. j = 10, i = 6
B. j is unspecified due to undefined behavior; result is not guaranteed
C. j = 11, i = 7
D. j = 12, i = 7

44 What does printf("%d\n", (int)(3.99 + 2.99)); print, and why?

type conversion and type modifiers Hard
A. 5
B. 6
C. 6.98
D. 7

45 For int a = 0, b = 5; int r = a++ && ++b;, what are the final values of r, a, and b?

logical operator Hard
A. r = 0, a = 1, b = 6
B. r = 1, a = 1, b = 6
C. r = 0, a = 1, b = 5
D. r = 0, a = 0, b = 5

46 Given input 12 34 (with a space), what does this code print?
c
int a, b;
scanf("%d%d", &a, &b);
printf("%d", a + b);

unformatted and formatted i/o functions- printf(), scanf(), puts(), gets(), getchar(), putchar() Hard
A. 1234
B. Compilation error
C. 12
D. 46

47 What is the value of x after int x = 4; x = (x > 2) ? (x < 8 ? x * 2 : x / 2) : x + 1;?

assignment and conditional operator Hard
A. 4
B. 2
C. 8
D. 5

48 On a system with 8-bit char, what does unsigned char c = 300; printf("%d", c); print?

data types Hard
A. -44
B. 44
C. 255
D. 300

49 What is the output of printf("%d", 1 << 3 | 1 << 1);?

bitwise operators Hard
A. 10
B. 6
C. 8
D. 16

50 What does the following print on a typical system?
c
unsigned int u = 10;
int i = -1;
if (i < u) printf("less"); else printf("not less");

type conversion and type modifiers Hard
A. Implementation-defined, always undefined
B. not less
C. Compilation error
D. less

51 Which of the following is a valid C identifier?

identifiers and keywords Hard
A. double
B. _count2
C. 2count
D. my-var

52 What is the value of the expression 5 + 3 * 2 % 4 - 1?

expressions Hard
A. 6
B. 3
C. 0
D. 8

53 What is the output of printf("%d", 010 + 0x10);?

constants and variables Hard
A. 24
B. 26
C. 20
D. 110

54 What does printf("%d", printf("abc")); output?

unformatted and formatted i/o functions- printf(), scanf(), puts(), gets(), getchar(), putchar() Hard
A. 3abc
B. abc3
C. abc
D. abc0

55 What does printf("%d", 3 < 2 < 1); print?

relational operator Hard
A. Undefined
B. 1
C. 0
D. Compilation error

56 Which expression correctly toggles (flips) the 3rd bit (bit index 2, value 4) of an integer n while leaving other bits unchanged?

bitwise operators Hard
A. n & ~(1 << 2)
B. n & (1 << 2)
C. n ^ (1 << 2)
D. n | (1 << 2)

57 What is printed by float f = 0.1f; printf("%d", f == 0.1);? (Assume IEEE-754)

type conversion and type modifiers Hard
A. Undefined behavior
B. 1
C. 0
D. Compilation error

58 Given int a = 10; int b = -a++;, what are the values of b and a afterward?

unary operator Hard
A. b = -11, a = 11
B. b = -11, a = 10
C. b = -10, a = 10
D. b = -10, a = 11

59 What is the output of int x = 5; printf("%d %d", x != 0, !x);?

expressions Hard
A. 0 1
B. 1 5
C. 1 0
D. 5 0

60 On a system where int is 4 bytes, what does printf("%d", sizeof('A')); most likely print in a C program (not C++)?

data types Hard
A. 1
B. 4
C. 2
D. 8