Home Subjects Computer Knowledge

Computer Knowledge

Programming, networking, database and OS questions

150 Q 2 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 61–70 of 150
Topics in Computer Knowledge
All C Programming 200 Database/SQL 100
Q.61 Medium C Programming
What is the output of: int x = 5; int y = ++x; ?
A x = 5, y = 5
B x = 6, y = 6
C x = 6, y = 5
D x = 5, y = 6
Correct Answer:  B. x = 6, y = 6
EXPLANATION

The pre-increment operator ++x increments x first (from 5 to 6), then assigns the new value to y. So x = 6 and y = 6.

Test
Q.62 Medium C Programming
What is the output of: int a = 10, b = 20; int *p = &a, *q = &b; printf("%d", *p + *q);?
A 10
B 20
C 30
D Error
Correct Answer:  C. 30
EXPLANATION

*p dereferences to a (10) and *q dereferences to b (20). Adding them: 10 + 20 = 30. Pointer arithmetic is used correctly here.

Test
Q.63 Medium C Programming
In the expression: int arr[10]; what is arr[5]?
A A pointer to the 5th element
B The value at memory address arr + 5
C Both A and B are equivalent
D An integer value between 0-10
Correct Answer:  C. Both A and B are equivalent
EXPLANATION

arr[5] accesses the element at index 5, which is at memory address (arr + 5). Array name arr acts as a pointer to the first element.

Test
Q.64 Medium C Programming
What is the return type of malloc() function?
A int*
B char*
C void*
D size_t
Correct Answer:  C. void*
EXPLANATION

malloc() returns a void pointer (void*), which can be cast to any data type pointer as needed by the programmer.

Test
Q.65 Medium C Programming
What is the purpose of the const keyword when used with a pointer?
A To make the pointer variable unchangeable
B To make the pointed data unchangeable
C Both A and B depending on placement
D To allocate memory in the constant segment
Correct Answer:  C. Both A and B depending on placement
EXPLANATION

const int *ptr makes the data constant (const int * ptr). int * const ptr makes the pointer constant. The position of const matters.

Test
Q.66 Medium C Programming
Which of the following correctly uses the ternary operator?
A x = (a > b) : a : b;
B x = (a > b) ? a : b;
C x = a > b ? a : b
D x = (a > b) -> a : b;
Correct Answer:  B. x = (a > b) ? a : b;
EXPLANATION

The ternary operator syntax is: condition ? value_if_true : value_if_false. Option B is the only correctly formatted statement.

Test
Q.67 Medium C Programming
What will be printed by: int x = 5; printf("%d", ++x + x++);?
A 11
B 12
C 13
D 14
Correct Answer:  B. 12
EXPLANATION

# Understanding Pre-increment and Post-increment Operators in C

The key to solving this problem is understanding the difference between pre-increment (++x) and post-increment (x++) operators and their evaluation order.

Step 1: Evaluate Pre-increment (++x)

The pre-increment operator increases the variable first, then uses the updated value in the expression.

\[\text{++x means: } x = x + 1 \text{ first, then use the new value}\]

When ++x is evaluated, x changes from 5 to 6, and the expression uses 6.

Step 2: Evaluate Post-increment (x++) and Addition

The post-increment operator uses the current value in the expression first, then increments the variable afterward.

\[\text{Expression becomes: } 6 + 6 \text{ (where first 6 is from ++x, second 6 is current value of x before x++ takes effect)}\]

After the entire expression is evaluated, x++ increments x from 6 to 7, but this happens after the addition is complete.

Step 3: Calculate Final Result

\[6 + 6 = 12\]

The answer is (B) 12

Test
Q.68 Medium C Programming
What is the correct syntax to pass a 2D array to a function in C?
A func(int arr[][])
B func(int arr[rows][cols])
C func(int arr[][cols])
D func(int **arr)
Correct Answer:  C. func(int arr[][cols])
EXPLANATION

When passing a 2D array to a function, the number of columns must be specified, but the number of rows is optional. Option C is the standard correct syntax.

Test
Q.69 Medium C Programming
What is the output of the expression: 10 / 3 * 3 in C?
A 10
B 9
C 3.33
D Cannot be determined
Correct Answer:  B. 9
EXPLANATION

Division and multiplication have the same precedence and are evaluated left-to-right. Step 1: 10 / 3 = 3 (integer division). Step 2: 3 * 3 = 9. Therefore, the result is 9.

Test
Q.70 Medium C Programming
Which of the following is a correct way to initialize a pointer to NULL?
A int *ptr = 0;
B int *ptr = NULL;
C int *ptr = (void*)0;
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

All three methods are correct ways to initialize a pointer to NULL. 0, NULL, and (void*)0 all represent a null pointer. NULL is typically a macro defined as 0 or ((void*)0) in the standard library.

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