Computer Knowledge — C Programming
Programming, networking, database and OS questions
200 Questions 5 Topics Take Test
Advertisement
Showing 191–200 of 200 questions in C Programming
Q.191 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.192 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.193 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.194 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.195 Easy C Programming
What does the strlen() function return?
A Number of characters in a string including null terminator
B Number of characters in a string excluding null terminator
C Size of memory allocated to string
D Address of the first character
Correct Answer:  B. Number of characters in a string excluding null terminator
EXPLANATION

strlen() returns the length of a string without counting the null terminator ('\0'). For example, strlen("hello") returns 5, not 6.

Take Test
Q.196 Easy C Programming
What is the output of the following C code?
int a = 10, b = 20;
int c = (a > b) ? a : b;
printf("%d", c);
A 10
B 20
C Compilation error
D 30
Correct Answer:  B. 20
EXPLANATION

The ternary operator (condition ? true_value : false_value) evaluates the condition (a > b). Since 10 is not greater than 20, it returns b which is 20.

Take Test
Q.197 Easy C Programming
What is the purpose of the 'void' keyword in C?
A To declare a function that returns nothing
B To declare a generic pointer
C Both A and B
D To declare empty variables
Correct Answer:  C. Both A and B
EXPLANATION

void is used in two contexts: (1) as a function return type when a function doesn't return a value, and (2) to declare a generic pointer (void *) that can point to any data type.

Take Test
Q.198 Easy C Programming
What will be the size of the following array in bytes?
char arr[10];
A 10 bytes
B 20 bytes
C 40 bytes
D 5 bytes
Correct Answer:  A. 10 bytes
EXPLANATION

Each char occupies 1 byte in memory. An array of 10 chars will occupy 10 × 1 = 10 bytes.

Take Test
Q.199 Easy C Programming
Which of the following is the correct syntax to declare a pointer in C?
A int *ptr;
B int& ptr;
C int ^ptr;
D int @ptr;
Correct Answer:  A. int *ptr;
EXPLANATION

In C, pointers are declared using the asterisk (*) symbol before the variable name. The syntax is 'datatype *pointer_name;'

Take Test
Q.200 Easy C Programming
What is the output of the following C code?
int x = 5;
printf("%d", x++);
A 6
B 5
C Compilation error
D Undefined behavior
Correct Answer:  B. 5
EXPLANATION

The post-increment operator (x++) returns the current value of x before incrementing. So printf prints 5, and then x becomes 6.

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