Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

490 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 421–430 of 490
Topics in C Programming
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
In the declaration 'int arr[10], *ptr;', which statement is correct?
A ptr is an array pointer, arr is an integer pointer
B ptr is a pointer to int, arr is an array of 10 integers
C Both arr and ptr are integer pointers
D arr is a pointer, ptr is an array
Correct Answer:  B. ptr is a pointer to int, arr is an array of 10 integers
EXPLANATION

The declaration uses comma separation. 'int arr[10]' declares an array of 10 integers, while '*ptr' declares a pointer to int.

Test
What is the size of int data type on a 32-bit system according to C standard?
A 2 bytes
B 4 bytes
C At least 2 bytes (implementation-dependent)
D 8 bytes
Correct Answer:  C. At least 2 bytes (implementation-dependent)
EXPLANATION

The C standard guarantees int is at least 2 bytes, but most 32-bit systems use 4 bytes. The exact size is implementation-dependent.

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