Quiz on C and C++ Language

1) What is the result of sizeof('a') in C/C++?

a) 1

b) 2

c) 4

d) Compiler-dependent

Answer: a) 1

2) What does the volatile keyword signify in C/C++?

a) The variable's value cannot be changed

b) The variable might be changed by external processes

c) The variable cannot be used in calculations

d) The variable is a constant

Answer: b) The variable might be changed by external processes

3) Which header file is needed for dynamic memory allocation in C/C++?

a) <stdlib.h>

b) <malloc.h>

c) <memory.h>

d) <stdio.h>

Answer: a) <stdlib.h>

4) In C/C++, what is the purpose of the const keyword?

a) It makes a variable unchangeable

b) It declares a variable that can only be initialized at compile time

c) It specifies that a function cannot modify an object

d) It is used to define constant values

Answer: a) It makes a variable unchangeable

5) Which operator in C/C++ is used to allocate memory dynamically?

a) new

b) alloc

c) malloc

d) allocate

Answer: c) malloc

6) What is the function of the break statement in C/C++?

a) Exits the loop or switch statement

b) Skips the next iteration of the loop

c) Jumps to a specific label in the code

d) Ends the program execution

Answer: a) Exits the loop or switch statement

7) In C++, what is the default access specifier for members of a class?

a) public

b) private

c) protected

d) friend

Answer: b) private

8) What is the output of printf("%d", sizeof(int)) in C/C++?

a) 2

b) 4

c) 8

d) Compiler-dependent

Answer: d) Compiler-dependent

9) Which of the following is a preprocessor directive in C/C++?

a) using

b) define

c) try

d) virtual

Answer: b) define

10) What does the -> operator do in C/C++?

a) Accesses the value pointed to by a pointer

b) Performs subtraction

c) Accesses the address of a variable

d) Calls a member function

Answer: a) Accesses the value pointed to by a pointer

11) Which loop in C/C++ executes at least once, even if the condition is false?

a) for loop

b) while loop

c) do-while loop

d) switch loop

Answer: c) do-while loop

12) What is the purpose of the sizeof operator in C/C++?

a) Returns the size of a variable or data type

b) Returns the memory address of a variable

c) Returns the value of a variable

d) Returns the maximum size of a variable

Answer: a) Returns the size of a variable or data type

13) What is the difference between malloc() and calloc() in C/C++?

a) malloc() initializes memory to zero, calloc() doesn’t.

b) calloc() initializes memory to zero, malloc() doesn’t.

c) Both functions initialize memory to zero.

d) Neither function initializes memory to zero.

Answer: b) calloc() initializes memory to zero, malloc() doesn’t.

14) In C/C++, what is the purpose of the static keyword when used with a global variable?

a) Limits the variable's scope to the current file

b) Makes the variable constant

c) Allocates memory dynamically

d) Allows access to the variable from other files

Answer: a) Limits the variable's scope to the current file

15) Which statement is true regarding the usage of delete in C++?

a) delete is used to free memory allocated by malloc()

b) delete is used to deallocate memory allocated by new

c) delete is used to release memory allocated by calloc()

d) delete is used to clear memory allocated by realloc()

Answer: b) delete is used to deallocate memory allocated by new

Explore Categories

JAVA

BOOTSTRAP

PYTHON

JAVASCRIPT

PHP

REACT

CSS

C & C++

HOMEPAGE