1.
What is the output of the following code?
#include<stdio.h>
main()
{
int n,i;
n=f(6);
printf("%d",n);
}
f(int x)
{
if(x==2)
return 2;
else
{
printf("+");
f(x-1);
}
}
Correct Answer
A. ++++2
Explanation
The code is using recursion to print a series of "+" symbols and then return the value 2. The function f(x) is called multiple times with decreasing values of x until x reaches 2. Each time the function is called, it prints a "+" symbol. Once x reaches 2, the function returns 2. Therefore, the output of the code will be a series of "+" symbols followed by the number 2.
2.
Which is correct with respect to the size of the data types?
Correct Answer
C. Char < int < double
Explanation
The correct order of the size of the data types is char < int < double. This means that char takes up the least amount of memory, followed by int, and then double.
3.
#include <stdio.h>
int main()
{
reverse(1);
}
void reverse(int i)
{
if (i > 5)
exit(0);
printf("%d\n", i);
return reverse(i++);
}
Correct Answer
D. Stack Overflow
Explanation
The given code will result in a stack overflow error. This is because the function reverse is being recursively called without any base case to stop the recursion. As a result, the function will continue to call itself indefinitely, causing the stack to overflow and resulting in a runtime error.
4.
What is function srand(unsigned)?
Correct Answer
A. Sets the seed for rand
Explanation
The function srand(unsigned) is used to set the seed for the rand function in C++. The seed is a value that initializes the random number generator, allowing for the generation of different sequences of random numbers. By setting the seed using srand, we can ensure that the sequence of random numbers generated by rand is different each time the program is run.
5.
#define FALSE -1
#define TRUE 1
#define NULL 0
main()
{
if(NULL)
puts("NULL");
else if(FALSE)
puts("TRUE");
else
puts("FALSE");
}
Correct Answer
B. TRUE
Explanation
The code defines the values of FALSE, TRUE, and NULL as -1, 1, and 0 respectively. In the if statement, NULL is evaluated as false because it has a value of 0. Therefore, the code moves to the else if statement. Since FALSE is defined as -1, it evaluates to true and the code executes the statement "puts("TRUE")". Therefore, the output of the code will be "TRUE".
6.
Void main()
{
int a,b;
a=3,1;
b=(5,4);
printf("%d",a+b);
}
Correct Answer
D. 7
Explanation
In the given code, there are two variables, 'a' and 'b', which are both integers.
In the line 'a=3,1;', there seems to be a typographical error. Instead of using a comma operator, it should be a semicolon. Therefore, the value of 'a' will be assigned as 3.
In the line 'b=(5,4);', there is a comma operator used. The comma operator evaluates both expressions and returns the value of the second expression. So, the value of 'b' will be assigned as 4.
Finally, the sum of 'a' and 'b' is printed, which is 7.
7.
Int main()
{
int a;
int b=5;
a=0 && --b;
printf("%d%d",a,b);
}
Correct Answer
A. 0 5
Explanation
In this code, the variable 'a' is assigned the value of the logical AND operation between 0 and the pre-decremented value of 'b'. The logical AND operator returns 0 if any of the operands is 0. Since 'b' is initially 5, it is decremented to 4 but this does not affect the value of 'a' as 0 is already 0. Therefore, 'a' remains 0 and 'b' remains 5. The correct answer is 0 5.
8.
Void Main()
{
int a=10;
printf("%d", a);
}
Correct Answer
A. Run Time Error
Explanation
This code will result in a runtime error because the printf function is being used without including the necessary header file. The printf function is part of the standard input/output library, which is declared in the header file "stdio.h". Without including this header file, the compiler will not recognize the printf function, leading to a runtime error.
9.
Int main(void)
{
while(".")
{
printf("Quest");
}
return 0;
}
Correct Answer
A. Quest(Infinity Times)
Explanation
The given code is an infinite loop because the condition of the while loop is a non-null string literal ("."). Since the condition is always true, the loop will continue indefinitely. Inside the loop, the code prints "Quest" repeatedly. Therefore, the output will be "Quest" repeated infinitely.
10.
Int main()
{
char str[10]="Quest";
printf("%d,%d\n",strlen(str),sizeof(str));
return 0;
}
Correct Answer
A. 5,10
Explanation
The code snippet declares a character array `str` with a size of 10 and initializes it with the string "Quest". The `strlen` function is used to determine the length of the string, which is 5 characters. The `sizeof` function is used to determine the size of the character array, which is 10 bytes. Therefore, the correct answer is 5,10.
11.
#include <stdio.h>
int main()
{
int x = 2, y = 0;
int z = y && (y |= 10);
printf("%d\n", z);
return 0;
}
Correct Answer
B. 0
Explanation
The code snippet is using the logical AND operator (&&) to evaluate the expression "y && (y |= 10)". In this expression, the value of y is first evaluated, which is 0. Then, the second part of the expression is evaluated, which is "y |= 10". This expression is performing a bitwise OR operation between y and 10, and then assigning the result back to y. However, since the value of y is 0, the bitwise OR operation will not change the value of y. Finally, the overall expression evaluates to 0 because the logical AND operator requires both operands to be true, but since the value of y is 0, it is considered false. Therefore, the value of z is 0.
12.
Main(int arg,char **argv)
{
printf("Enter the character");
getchar();
sum(argv[1],argv[2]);
}
sum(num1,num2)
int num1,num2;
{
return num1+num2;
}
Correct Answer
B. Compile Error
Explanation
The given code will result in a compile error because the function sum() is not declared before it is used in the main() function. In C, functions need to be declared before they are used, either by including a function prototype at the beginning of the file or by defining the function before it is used. Since the sum() function is defined after it is used in the main() function, the compiler will not be able to recognize it and will throw a compile error.
13.
_______ occurs when a result is too large in magnitude to represent errors as a floating-point value of the required type.
Correct Answer
A. Underflow
Explanation
Underflow occurs when a result is too small in magnitude to be represented accurately as a floating-point value of the required type. In other words, it happens when a number is so close to zero that it cannot be precisely represented using the available number of bits. This can lead to loss of precision and inaccuracies in calculations.
14.
Which one do you like?#include <stdio.h>
int main()
{
int i = 0;
int j = i++ + i;
printf("%d\n", j);
}
Correct Answer
C. 1
Explanation
The given code snippet initializes two variables, i and j, with i being assigned a value of 0. The expression i++ + i is then evaluated and assigned to j. The post-increment operator (i++) increments the value of i after it has been used in the expression, so j is assigned the value of 0 + 1, which is 1. Finally, the value of j is printed, resulting in the output of 1.
15.
Void test(struct number n)
{
n.x=100;
}
struct number{ int x; };
int main()
{
struct number num;
test(num);
printf("%d\n",num.x);
return 0;
}
Correct Answer
D. Error
Explanation
The code is attempting to pass a struct as an argument to a function, but the struct is not defined before the function prototype. This results in a compilation error because the compiler does not know the definition of the struct when it encounters the function call. Therefore, the correct answer is "Error".
16.
Int main()
{
int x=5;
if(x>=10)
printf("Quest");
printf("CSE");
else printf("2K19");
}
Correct Answer
B. Compilation Error
Explanation
The given code will result in a compilation error. This is because the ">" symbol is not properly encoded as ">" in the code. Therefore, the code will not be able to compile and will result in a compilation error.
17.
In the absence of a exit condition in a recursive function, the following error is given __________
Correct Answer
B. Run Time Error
Explanation
In the absence of an exit condition in a recursive function, the function will keep calling itself indefinitely, leading to a stack overflow. This will result in a runtime error as the program will run out of memory to allocate for the function calls.
18.
Examination of the program step by step is called ______________
Correct Answer
B. Stepping
Explanation
Stepping refers to the process of examining a program step by step, allowing for a detailed analysis of its execution. It involves pausing the program at each step to observe the values of variables, the flow of control, and the effects of each instruction. This technique is commonly used in debugging to identify and fix errors in the code. By carefully stepping through the program, programmers can gain a better understanding of its behavior and pinpoint any issues that may be causing unexpected results.
19.
#include<stdio.h>
int main()
{
int fun={
printf("C for Loop");
};
int x=5;
for(x=0;x<=fun;x++)
{
printf("%x",x);
}
}
Correct Answer
C. Compile Time Error
Explanation
The code provided has a syntax error. The variable "fun" is declared as an integer but is assigned a block of code instead of a value. This is not valid in C programming. Therefore, a compile-time error will occur.
20.
#include<stdio.h>
#define x 5+2
int main( )
{
int i;
i=x*x*x;
printf("%d",i);
return 0;
}
Correct Answer
A. 27
Explanation
The program defines a macro `x` as `5+2`. In the `main` function, the variable `i` is assigned the value of `x*x*x`, which is equivalent to `5+2*5+2*5+2`, which simplifies to `5+10+10+2`, resulting in `27`. The program then prints the value of `i`, which is `27`.