Home Subjects C Programming Data Types & Variables

C Programming
Data Types & Variables

C language from basics to advanced placement prep

52 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 52
Topics in C Programming
Which of the following declarations allocates memory for a variable?
A extern int x;
B int x;
C static int x;
D Both B and C
Correct Answer:  D. Both B and C
EXPLANATION

Both 'int x;' and 'static int x;' allocate memory. Extern only declares without allocation.

Test
What is the output of the following code?
int x = 10;
int *p = &x;
printf("%d", *p + 5);
A 10
B 15
C 5
D Compilation error
Correct Answer:  B. 15
EXPLANATION

*p dereferences pointer to get value 10, then 10+5=15 is printed.

Test
Which keyword is used to make a variable retain its value between function calls?
A extern
B static
C volatile
D const
Correct Answer:  B. static
EXPLANATION

Static variables retain their values between function calls and are initialized only once, maintaining state across invocations.

Test
Which format specifier is used for printing a long integer?
A %d
B %ld
C %lld
D %D
Correct Answer:  B. %ld
EXPLANATION

%ld is for long int, %lld is for long long int, and %d is for regular int. Using wrong specifier can cause undefined behavior.

Test
What will happen if you declare a variable without initializing it?
A It will have a garbage value (undefined)
B It will be automatically initialized to 0
C Compilation error occurs
D It will be initialized to NULL
Correct Answer:  A. It will have a garbage value (undefined)
EXPLANATION

Uninitialized local variables contain garbage values (random data from memory). Static/global variables are initialized to 0 by default.

Test
In C, what is the result of: int x = 5.7?
A Compilation error
B x = 5 (implicit truncation)
C x = 5.7
D x = 6 (rounded)
Correct Answer:  B. x = 5 (implicit truncation)
EXPLANATION

When assigning a double to int, C performs implicit type conversion with truncation (not rounding). The decimal part is discarded.

Test
What is the difference between 'char' and 'unsigned char' for character representation?
A No difference; they are aliases
B char can hold negative values; unsigned char cannot
C unsigned char can hold larger ASCII values (128-255)
D Both B and C are correct
Correct Answer:  D. Both B and C are correct
EXPLANATION

char (signed) has range -128 to 127, while unsigned char has 0 to 255. The latter can represent extended ASCII characters (128-255).

Test
What is the output of: printf("%d", sizeof(int) + sizeof(char))?
A 4
B 5
C Depends on system (at least 3)
D 8
Correct Answer:  C. Depends on system (at least 3)
EXPLANATION

# Understanding `sizeof()` Operator in C

The `sizeof()` operator returns the size of data types in bytes, which varies depending on the compiler and system architecture.

Step 1: Analyze sizeof(int)

In C, the size of `int` is implementation-defined and depends on the system architecture and compiler.

\[\text{sizeof(int)} = \begin{cases} 2 \text{ bytes (16-bit systems)} \\ 4 \text{ bytes (32-bit/64-bit systems)} \\ \text{or other values} \end{cases}\]

Step 2: Analyze sizeof(char) and Calculate Total

The `char` data type always occupies exactly 1 byte across all standard C implementations, but `int` varies by platform.

\[\text{Result} = \text{sizeof(int)} + \text{sizeof(char)} = \text{sizeof(int)} + 1\]

For a 16-bit system: \(2 + 1 = 3\) bytes

For a 32-bit system: \(4 + 1 = 5\) bytes

For a 64-bit system: \(4 + 1 = 5\) bytes (typically)

The output depends on the system's architecture. The minimum guaranteed value is 3 bytes (on 16-bit systems), but it can be higher on modern systems.

Correct Answer: (C) Depends on system (at least 3)

Test
What happens when you assign a larger data type value to a smaller one?
A Compilation error occurs
B Implicit type conversion (with possible data loss)
C Runtime error occurs
D The value is automatically promoted
Correct Answer:  B. Implicit type conversion (with possible data loss)
EXPLANATION

C allows implicit type conversion. When assigning larger to smaller type, truncation or data loss may occur without any compiler error.

Test
Which of the following correctly declares a constant integer variable?
A const int x = 10;
B int const x = 10;
C Both A and B are correct
D constant int x = 10;
Correct Answer:  C. Both A and B are correct
EXPLANATION

Both 'const int' and 'int const' are valid and equivalent ways to declare a constant integer in C.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips