Table of contents
- 1) What will be the output of the following code snippet?
- 2) What will be the output of the following code snippet?
- 3) Output ?
- 4) Output ?
- 5) Output ?
- 6)
- 7) Which of the following is the proper syntax for declaring macros in C?
- 8) Which of the following will occur if we call the free() function on a NULL pointer?
- 9) What will be the output of the following code snippet?
- 10) Which language does not allow function overloading ?
- 11) The translator which performs macro call expansion is called ?
- 12)Which of the following describes the primary use of the #pragma once directive in C and C++ header files?
- 13) What type of file is generated after the preprocessing stage of a C program?
- 14) What will happen if a header file is included multiple times in a C program?
- Explanation
- 15) Output
- 16) What is the purpose of the declaration int x:8; in C?
- 17) Why can't a function in C return an array directly?
- 18) Which of the following array declarations in C are valid?
- Explanation
- 19) Why can the compiler infer the size of the first dimension of an array but not the second dimension when using an initializer list?
- 20) Why is the declaration int arr[][3] = {1,2,3,4}; invalid in C?
- 21) Output ?
- 22) What will be the output of the program ?
- 23) What will be the output of the program ?
- 24) Which standard library function will you use to find the last occurance of a character in a string in C?
- 25)
1) What will be the output of the following code snippet?
#include <stdio.h>
void solve() {
int x = 2;
printf("%d", (x << 1) + (x >> 1));
}
int main() {
solve();
return 0;
}
Explanation: The above example uses the knowledge of bitwise shift operators. (x << 1) is equivalent to x 2 and (x >> 1) is equivalent to x / 2. So the result is 2 2 + 2 / 2 = 5.
2) What will be the output of the following code snippet?
#include <stdio.h>
#define CUBE(x) x * x * x
void solve() {
int ans = 216 / CUBE(3);
printf("%d", ans);
}
int main() {
solve();
return 0;
}
The macro CUBE(x) is defined as x * x * x.
When you use this macro in the expression 216 / CUBE(3),
it gets expanded to 216 / 3 * 3 * 3.
216 / 3 = 72
72 * 3 = 216
216 * 3 = 648
However, the code should be corrected to properly calculate the cube of 3
by using parentheses in the macro definition:
#define CUBE(x) ((x) * (x) * (x))
With this corrected macro definition, the expression
216 / CUBE(3) will be properly expanded and evaluated as:
216 / ((3) * (3) * (3))
3) Output ?
#include <stdio.h>
void solve() {
int n = 24;
int l = 0, r = 100, ans = n;
while(l <= r) {
int mid = (l + r) / 2;
if(mid * mid <= n) {
ans = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
printf("%d", ans);
}
int main() {
solve();
return 0;
}
Explanation: The code snippet basically uses binary search to calculate the floor of the square root of a number. Since the square root is an increasing function, so binary search is applicable here. Here, for n = 24, the answer is 4.
4) Output ?
#include <stdio.h>
void solve() {
bool ok = false;
printf(ok ? "YES" : "NO");
}
int main() {
solve();
return 0;
}
output : NO
Explanation: The bool data type cannot be used in C without including the
stdbool.h header.
5) Output ?
#include <stdio.h>
void solve() {
int ch = 2;
switch(ch) {
case 1: printf("1 ");
case 2: printf("2 ");
case 3: printf("3 ");
default: printf("None");
}
}
int main() {
solve();
return 0;
}
output :
2 3 None
Wrong Answer
Explanation: This is an example of Fall-Though in switch statements in absence
of break statements.
important : it would not be 1 2 3 None
6)
#include <stdio.h>
void solve() {
int x = printf("Hello");
printf(" %d", x);
}
int main() {
solve();
return 0;
}
The printf function returns the number of characters printed to the standard
output, which here is 5.
7) Which of the following is the proper syntax for declaring macros in C?
#define MACRO_NAME macro_definition
8) Which of the following will occur if we call the free() function on a NULL pointer?
Runtime
Compile Time
Undefined behaviour
Executed normally
Explanation: Calling the free() function on a NULL pointer is totally valid and will not cause any error.
9) What will be the output of the following code snippet?
#include <stdio.h>
union School {
int age, rollNo;
double marks;
};
void solve() {
union School sc;
sc.age = 19;
sc.rollNo = 82;
sc.marks = 19.04;
printf("%d", (int)sizeof(sc));
}
int main() {
solve();
return 0;
}
Explanation: The size of a Union is equal to the size of the largest
variable which is a part of it. Here the variable is double which of size 8bytes.
10) Which language does not allow function overloading ?
Java
CPP
C
Python
answer : C, as it is not oop language.
11) The translator which performs macro call expansion is called ?
ans : = macro pre-processor
12)Which of the following describes the primary use of the #pragma once
directive in C and C++ header files?
A) It defines a macro for conditional compilation.
B) It prevents the header file from being included multiple times in a single translation unit.
C) It replaces the standard include guards for ensuring a header file is included only once.
D) It automatically generates documentation for the header file.
Answer: B) It prevents the header file from being included multiple times in a single translation unit.
13) What type of file is generated after the preprocessing stage of a C program?
A) Object file
B) Assembly file
C) Preprocessed source file
D) Executable file
Answer: C) Preprocessed source file
14) What will happen if a header file is included multiple times in a C program?
A) The program will fail to compile with an error.
B) The program will include the header file content multiple times, causing redefinition errors.
C) The header file content will be included only once due to include guards or #pragma once
.
D) The program will run but produce incorrect results.
Answer: C) The header file content will be included only once due to include guards or #pragma once
.
Explanation
Modern C header files use include guards or #pragma once
to ensure that their content is included only once per compilation unit, preventing redefinition errors and ensuring correct compilation. Thus, including the same header file multiple times does not cause compilation errors.
15) Output
#include<stdio.h>
int main()
{
int n = printf("hello");
printf("%d",n);
}
ans:- hello5, because printf returns the number of character printed.
16) What is the purpose of the declaration int x:8;
in C?
A) To declare x
as an integer variable with a size of 8 bytes.
B) To define x
as a bit field of type int
that uses 8 bits.
C) To create an array of 8 integers named x
.
D) To declare x
as a pointer to an 8-bit integer.
Answer: B) To define x
as a bit field of type int
that uses 8 bits.
17) Why can't a function in C return an array directly?
A) Arrays are not supported in C functions.
B) C does not allow functions to return more than one value.
C) Arrays do not have a fixed size in C, making it impossible to return them directly.
D) C functions cannot return arrays because arrays are not first-class citizens in C; instead, pointers to arrays are returned.
Answer: D) C functions cannot return arrays because arrays are not first-class citizens in C; instead, pointers to arrays are returned.
18) Which of the following array declarations in C are valid?
A) int arr[][] = {1,2,3,4};
B) int arr[2][] = {1,2,3,4};
C) int arr[][2] = {1,2,3,4};
D) int arr[2][2] = {1,2,3,4};
Answer: C) int arr[][2] = {1,2,3,4};
and D) int arr[2][2] = {1,2,3,4};
Explanation
int arr[][2] = {1,2,3,4};
is valid because the compiler can infer the size of the first dimension based on the provided second dimension and initializer.int arr[2][2] = {1,2,3,4};
is valid because both dimensions are explicitly specified and correctly initialized.
19) Why can the compiler infer the size of the first dimension of an array but not the second dimension when using an initializer list?
A) The compiler can only infer dimensions based on the size of the array data type.
B) The compiler cannot infer the second dimension because it needs to know how to organize the data into rows and columns.
C) The compiler always requires explicit size for all dimensions in an array declaration.
D) The compiler assumes the second dimension size is zero if not specified.
Answer: B) The compiler cannot infer the second dimension because it needs to know how to organize the data into rows and columns.
20) Why is the declaration int arr[][3] = {1,2,3,4};
invalid in C?
A) The size of the second dimension must be specified along with the size of the first dimension.
B) The initializer list does not match the required number of elements for the given second dimension.
C) The array cannot have more than one dimension.
D) The array elements must be of type int
.
Answer: B) The initializer list does not match the required number of elements for the given second dimension.
21) Output ?
#include<stdio.h>
int main()
{
printf("%d %d %d",(023),(23),(0x56));
}
ans :- 19 23 56
numbers starting with 0 are octal in C and with 0x are hex
22) What will be the output of the program ?
#include<stdio.h>
int main()
{
char t;
char *p1 = "India", *p2;
p2=p1;
p1 = "BIX";
printf("%s %s\n", p1, p2);
return 0;
}
In the provided C code, the printf
statement outputs "BIX India"
. Initially, p1
is set to point to the string literal "India"
, and p2
is then assigned the value of p1
, so p2
also points to "India"
. When p1
is later reassigned to point to a different string literal "BIX"
, p2
still retains its original value, pointing to "India"
. As a result, when the printf
function is called, it prints the current value of p1
(which is "BIX"
) followed by the value of p2
(which is "India"
), producing the output "BIX India"
.
23) What will be the output of the program ?
#include<stdio.h>
int main()
{
enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
printf("%d, %d, %d, %d, %d, %d\n", MON, TUE, WED, THU, FRI, SAT);
return 0;
}
MON
is explicitly assigned the value-1
.TUE
will automatically get the valueMON + 1
, which is-1 + 1 = 0
.WED
is explicitly assigned the value6
.THU
will automatically get the valueWED + 1
, which is6 + 1 = 7
.FRI
will automatically get the valueTHU + 1
, which is7 + 1 = 8
.SAT
will automatically get the valueFRI + 1
, which is8 + 1 = 9
.
24) Which standard library function will you use to find the last occurance of a character in a string in C?
strnchar()
strchar()
strrchar()
strrchr()
ans: strchr()
25)
int main()
{
while(1){
if(printf("%d",printf("%d")))
break;
else
continue;
}
return 0;
}
Here’s a summary of what printf returns:
Number of Characters Printed: The primary return value of printf is the total
number of characters that were output to the standard output. For example,
if you print "Hello" using printf, it returns 5 because "Hello" consists of
5 characters.
Formatting Effects: If you use printf to format the output, such as including
formatting specifiers like %d for integers or %s for strings, printf still
returns the count of characters that were actually printed after formatting.
-------------------------------------------------------------------------
int main()
{
unsigned int i=10;
while(i-- >= 0)
printf("%u ",i);
return 0;
}
Output:
9 8 7 6 5 4 3 2 1 0 4294967295 4294967294 …… (on a machine where int is 4 bytes long)
9 8 7 6 5 4 3 2 1 0 65535 65534 …. (on a machine where int is 2 bytes long)
---------------------------------------------------------------------------
int main()
{
int x, y = 2, z, a;
if (x = y % 2)
z = 2;
a = 2;
printf("%d %d ", z, x);
return 0;
}
Output:
0 0
Explanation:
This question has some stuff for operator precedence. If the condition of if
is met, then z will be initialized to 2 otherwise z 0(implicit initialization).
But the condition of if has two operators: assignment operator and modulus
operator. The precedence of modulus is higher than assignment. So y%2 is zero
and it’ll be assigned to x. So the value of x becomes zero which is also the
effective condition for if. And therefore, condition of if is false.
----------------------------------------------------------------------------
int main()
{
int a[10];
printf("%d",*a+1-*a+3);
return 0;
}
4 ( precedence of * operator is > than addition)
------------------------------------------------------------------------------
#define prod(a,b) a*b
int main()
{
int x=3,y=4;
printf("%d",prod(x+2,y-1));
return 0;
}
Output: x+2*y-1 = 3+2*4-1 = 10
-------------------------------------------------------------------------------
#include<stdio.h>
int main(void)
{
int a = 1;
int b = 0;
b = ++a + ++a;
printf("%d %d",a,b);
getchar();
return 0;
}
OUTPUT: 3 6 ( because ++a: a = 2, then other ++a: a=3, so at end line becomes
b = 3 + 3 (a=3)
-------------------------------------------------------------------------------
#include <stdio.h>
char* fun()
{
return "awake";
}
int main()
{
printf("%s",fun()+ printf("I see you"));
getchar();
return 0;
}
Output:
printf("I see you") = 9
"%s",fun() = address to "awake".
printf(address to awake + 9 )
--------------------------------------------------------------------------------
#include<stdio.h>
#define R 10
#define C 20
int main()
{
int (*p)[R][C];
printf("%d", sizeof(*p));
getchar();
return 0;
}
Output: 10*20*sizeof(int) which is “800” for compilers with integer size as 4
bytes.The pointer p is de-referenced, hence it yields type of the object.
In the present case, it is an array of array of integers. So, it prints
R*C*sizeof(int).
-------------------------------------------------------------------------------
#include<stdio.h>
#define f(g,g2) g##g2
int main()
{
int var12 = 100;
printf("%d", f(var,12));
getchar();
return 0;
}
Output: 100
The operator ## is called “Token-Pasting” or “Merge” Operator. It merges two tokens into one token. So, after preprocessing, the main function becomes as follows, and prints 100.
int main()
{
int var12 = 100;
printf("%d", var12);
getchar();
return 0;
}
-------------------------------------------------------------------------------
int main()
{
int var12 = 100;
printf("%d", var12);
getchar();
return 0;
}
Output: “same x is MAXUINT, y is MAXUINT” Where MAXUINT is the maximum
possible value for an unsigned integer.
-1 and ~0 essentially have same bit pattern, hence x and y must be same.
In the comparison, y is promoted to unsigned and compared against x.
The result is “same”. However, when interpreted as signed and unsigned
their numerical values will differ. x is MAXUNIT and y is -1. Since we
have %u for y also, the output will be MAXUNIT and MAXUNIT.
Thanks to Venki for explanation.
-------------------------------------------------------------------------------
#include<stdio.h>
int fun()
{
static int num = 40;
return num--;
}
int main()
{
for(fun(); fun(); fun())
{
printf("%d ", fun());
}
getchar();
return 0;
}
Output: 38 35 32 29 26 23 20 17 14 11 8 5 2
Since num is static in fun(), the old value of num is preserved for
subsequent functions calls. Also, since the statement return num– is postfix,
it returns the old value of num, and updates the value for next function call.
-+-+-++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 30;
if (c > b > a)
{
printf("TRUE");
}
else
{
printf("FALSE");
}
getchar();
return 0;
}
Output: FALSE
Let us consider the condition inside the if statement. Since there are two
greater than (>) operators in expression “c > b > a”, associativity of > is
considered. Associativity of > is left to right. So, expression c > b > a is
evaluated as ( (c > b) > a ) which is false.
---------------------------------------------------------------------------------
#include<stdio.h>
int main()
{
int i = 20,j;
i = (printf("Hello"), printf(" All Geeks "));
printf("%d", i);
return 0;
}
Output: Hello All Geeks 11 The printf() function returns the number of
characters it has successfully printed. The comma operator evaluates it
operands from left to right and returns the value returned by the rightmost
expression (See this for more details). First printf(“Hello”) executes and
prints “Hello”, the printf(” All Geeks “) executes and prints ” All Geeks “.
This printf statement returns 11 which is assigned to i. Please write comments
if you find any of the answers/explanations incorrect, or you want to share
more information about the topics discussed above
--------------------------------------------------------------------------------
#include<stdio.h>
int main()
{
enum channel {star, sony, zee};
enum symbol {hash, star};
int i = 0;
for(i = star; i <= zee; i++)
{
printf("%d ", i);
}
return 0;
}
Output:
compiler error: redeclaration of enumerator 'star'
In the above program, enumeration constant ‘star’ appears two times in main()
which causes the error. An enumeration constant must be unique within the scope
in which it is defined. The following program works fine and prints 0 1 2 as
the enumeration constants automatically get the values starting from 0.
#include<stdio.h>
int main()
{
enum channel {star, sony, zee};
int i = 0;
for(i = star; i <= zee; i++)
{
printf("%d ", i);
}
return 0;
}
Output:
0 1 2
--------------------------------------------------------------------------------