Computer Knowledge — C Programming
Aptitude · Reasoning · English · CS — Corporate & Campus Interview Prep
96 Questions 5 Topics Take Test
Advertisement
Showing 1–10 of 96 questions in C Programming
Q.1 Medium C Programming
What is the output of the following C code?
int x = 5;
int y = ++x + x++;
printf("%d", y);
A 11
B 12
C 13
D Undefined behavior
Correct Answer:  D. Undefined behavior
Explanation:

This code exhibits undefined behavior because x is modified twice between sequence points without an intervening sequence point. The result depends on the compiler's implementation.

Take Test
Q.2 Medium C Programming
What is the difference between scanf() and gets() functions?
A scanf() can read formatted input while gets() reads only strings
B gets() is safer than scanf()
C There is no difference
D scanf() stops at whitespace while gets() doesn't
Correct Answer:  A. scanf() can read formatted input while gets() reads only strings
Explanation:

scanf() reads formatted input based on format specifiers and stops at whitespace. gets() reads a string until a newline is encountered. Note: gets() is deprecated due to buffer overflow vulnerabilities.

Take Test
Q.3 Medium C Programming
What is the correct way to allocate memory for a single integer using malloc()?
A int *ptr = (int *)malloc(sizeof(int));
B int *ptr = malloc(sizeof(int));
C int *ptr = (int *)malloc(4);
D Both A and B
Correct Answer:  D. Both A and B
Explanation:

Both A and B are correct. Option A uses explicit type casting which is optional in C (not in C++). Option B avoids casting. Using sizeof(int) is preferred over hardcoding 4, as int size may vary across systems.

Take Test
Q.4 Medium C Programming
What is the output of the following C code?
int x = 5;
int *ptr = &x;
printf("%d %d", *ptr, x);
A 5 5
B Address Address
C Garbage value 5
D Compilation error
Correct Answer:  A. 5 5
Explanation:

*ptr dereferences the pointer to access the value at the address it points to, which is x = 5. So both *ptr and x print 5.

Take Test
Q.5 Medium C Programming
What happens when you use the strcpy() function without bounds checking?
A It prevents buffer overflow automatically
B It may cause buffer overflow if source string is longer than destination
C It returns an error code
D It truncates the source string automatically
Correct Answer:  B. It may cause buffer overflow if source string is longer than destination
Explanation:

strcpy() does not perform bounds checking. If the source string is longer than the destination buffer, it will write beyond the buffer boundary, causing a buffer overflow. This is a security vulnerability. Using strncpy() is safer.

Take Test
Advertisement
Q.6 Medium C Programming
What is the output of the following C code?
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d", *(ptr + 1));
A 10
B 20
C 30
D Address of second element
Correct Answer:  B. 20
Explanation:

ptr points to arr[0]. ptr + 1 points to arr[1]. *(ptr + 1) dereferences to get the value at arr[1] which is 20. Pointer arithmetic adds sizeof(int) to the address for each increment.

Take Test
Q.7 Medium C Programming
Which of the following correctly describes the scope of a static variable declared inside a function?
A Local to the function, retains value between function calls
B Global scope, initialized once
C Local to the file only
D Creates a new instance on each function call
Correct Answer:  A. Local to the function, retains value between function calls
Explanation:

A static variable declared inside a function has local scope (visible only within that function) but persists for the entire program lifetime. Its value is retained between function calls and is initialized only once.

Take Test
Q.8 Medium C Programming
What will be the output of the following C code?
int x = 10;
int y = 20;
int z = x < y ? x++ : y++;
printf("%d %d %d", x, y, z);
A 10 20 10
B 11 20 10
C 10 21 20
D 11 21 10
Correct Answer:  B. 11 20 10
Explanation:
Step 1: Evaluate condition x < y → 10 < 20 → true. Step 2: Execute true branch: x++ returns 10, then x becomes 11. Step 3: z = 10. Step 4: printf prints x=11, y=20, z=10.
Take Test
Q.9 Medium C Programming
In C, a pointer variable stores which of the following?
A The actual value of a variable
B The memory address of a variable
C The size of a variable
D The data type of a variable
Correct Answer:  B. The memory address of a variable
Explanation:

A pointer is a variable that stores the memory address of another variable. It is declared using the * symbol.

Take Test
Q.10 Medium C Programming
What is the purpose of the malloc() function in C?
A To deallocate memory
B To allocate memory dynamically at runtime
C To initialize variables
D To declare arrays
Correct Answer:  B. To allocate memory dynamically at runtime
Explanation:

malloc() (memory allocation) allocates a block of memory dynamically during program execution and returns a pointer to it.

Take Test
IGET
iget AI
Online · Ask anything about exams
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