Home Subjects C Programming Basics & Syntax

C Programming
Basics & Syntax

C language from basics to advanced placement prep

40 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 40
Topics in C Programming
Q.21 Medium Basics & Syntax
Which of the following is a correct way to read a string from user input avoiding buffer overflow?
A scanf("%s", str);
B scanf("%10s", str);
C gets(str);
D scanf("%[^ ]s", str);
Correct Answer:  B. scanf("%10s", str);
EXPLANATION

Using scanf("%10s", str) limits input to 10 characters, preventing buffer overflow. The gets() function is deprecated and unsafe.

Test
Q.22 Medium Basics & Syntax
What does the 'const' keyword do when applied to a pointer?
A Makes the pointer value constant only
B Makes the pointed data constant only
C Can make either or both constant depending on placement
D Has no effect in C
Correct Answer:  C. Can make either or both constant depending on placement
EXPLANATION

'const int *ptr' makes the data constant, 'int * const ptr' makes the pointer constant, and 'const int * const ptr' makes both constant.

Test
Q.23 Medium Basics & Syntax
What is the primary purpose of the 'volatile' keyword in C?
A To prevent variable modification
B To inform the compiler that a variable's value can change unexpectedly
C To allocate memory dynamically
D To declare function pointers
Correct Answer:  B. To inform the compiler that a variable's value can change unexpectedly
EXPLANATION

The 'volatile' keyword tells the compiler not to optimize accesses to a variable, as its value can change from external sources (hardware, signals, etc.).

Test
Q.24 Medium Basics & Syntax
Which of the following correctly represents a string literal in C?
A char str = 'Hello';
B char str[] = "Hello";
C string str = "Hello";
D char *str = 'Hello';
Correct Answer:  B. char str[] = "Hello";
EXPLANATION

Strings in C are represented using double quotes and stored in character arrays. Single quotes are for single characters.

Test
Q.25 Medium Basics & Syntax
What will be the output of the expression: 5/2 in C (considering integer division)?
A 2.5
B 3
C 2
D Error
Correct Answer:  C. 2
EXPLANATION

In C, when both operands are integers, division performs integer division, discarding the fractional part. 5/2 = 2.

Test
Q.26 Medium Basics & Syntax
What is the range of values for 'signed char' in C (assuming 8-bit char)?
A 0 to 255
B -128 to 127
C -256 to 255
D 0 to 127
Correct Answer:  B. -128 to 127
EXPLANATION

A signed char is 1 byte (8 bits) and can represent values from -128 to 127 using two's complement representation.

Test
Q.27 Medium Basics & Syntax
In C, what is the purpose of the 'static' keyword when used with a global variable?
A To make it accessible only within the current file
B To allocate it on the stack
C To make it a constant
D To prevent memory deallocation
Correct Answer:  A. To make it accessible only within the current file
EXPLANATION

When 'static' is used with a global variable, it restricts its scope to the current file (internal linkage), making it not accessible from other files.

Test
Q.28 Medium Basics & Syntax
Which memory segment stores global variables in C?
A Stack
B Heap
C Data segment
D Code segment
Correct Answer:  C. Data segment
EXPLANATION

Global variables are stored in the data segment (also called BSS segment for uninitialized globals). Local variables use the stack.

Test
Q.29 Medium Basics & Syntax
Which of the following is NOT a valid data type modifier in C?
A unsigned long
B signed double
C long float
D unsigned char
Correct Answer:  C. long float
EXPLANATION

The 'long' modifier cannot be used with 'float'. You can only use 'double' or 'long double' for longer floating-point types.

Test
Q.30 Medium Basics & Syntax
What is the output of the following code?
#include
int main() {
int a = 5, b = 10;
a = b++;
printf("%d %d", a, b);
return 0;
}
A 5 10
B 10 11
C 11 11
D 10 10
Correct Answer:  B. 10 11
EXPLANATION

b++ returns the current value of b (10) before incrementing, so a = 10. Then b increments to 11.

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