1.
What is output:
printf("%d" printf("hello") );
Correct Answer
C. Hello5
Explanation
The printf function is used to print the output. In this code, the inner printf function "printf("hello")" will print "hello" on the console. The outer printf function will then print the number of characters printed by the inner printf function, which is 5. Therefore, the output will be "hello5".
2.
What is output:
printf( "%d", printf('' %d" , printf("hii"));
Correct Answer
B. Hii31
Explanation
The code snippet uses nested printf statements. The innermost printf("hii") statement prints "hii" and returns the number of characters printed, which is 3. The next printf statement ("%d", 3) prints the value 3. Finally, the outermost printf statement ("%d", 3) prints the value 3 as well. So the overall output is "hii31".
3.
What is output:
int a = 10, b = 20, c = 30;
printf("%d %d %d");
Correct Answer
B. 30 20 10
Explanation
The correct answer is "30 20 10". The printf function is used to print the values of variables a, b, and c. The format specifier "%d" is used to print the values as integers. Since the variables are printed in the order a, b, c, the output will be 30 20 10.
4.
What is the output:
printf(" %d " , printf("%dhii", printf("hello")));
Correct Answer
B. Hello5hii4
Explanation
The output is "hello5hii4" because the printf function is being nested within itself. The innermost printf statement "printf("hello")" prints "hello" and returns the number of characters printed, which is 5. Then, the next printf statement "printf("%dhii", 5)" prints "5hii" and returns the number of characters printed, which is 4. Finally, the outermost printf statement "printf(" %d ", 4)" prints the value of 4 and returns the number of characters printed, which is also 4. Therefore, the overall output is "hello5hii4".
5.
In which line there is an error in the program :
1 int main()
2 {
3 char ch[10];
4 scanf("%s", ch);
5 printf("%s", ch);
6 return 0;
7 }
Correct Answer
C. 4
Explanation
The error in the program is in line 4. The format specifier used in the scanf function is "%s" which is used for reading a string. However, the variable "ch" is declared as a character array with a size of 10, which means it can only store a string of maximum length 9 (including the null character). This can lead to a buffer overflow if the user enters a string longer than 9 characters. To fix this error, the size of the character array should be increased or a limit should be imposed on the input string length.
6.
What is the value of num?
int num = 4/3*2.4;
Correct Answer
C. 2
Explanation
The value of num is 2 because the expression 4/3*2.4 is evaluated from left to right according to the order of operations. First, 4 divided by 3 equals 1.3333. Then, 1.3333 multiplied by 2.4 equals 3.2. However, since the variable num is declared as an int, the decimal portion is truncated, resulting in the value 3. Therefore, the correct answer is 2.
7.
What is the value of the num:
int num = -17 % 4;
Correct Answer
C. -1
Explanation
The value of the variable "num" is -1. The expression "-17 % 4" calculates the remainder when -17 is divided by 4. Since -17 divided by 4 is -4 with a remainder of -1, the value of "num" is -1.
8.
What is the value of the num:
int num = 17 % -4;
Correct Answer
B. 1
Explanation
The expression "17 % -4" calculates the remainder when 17 is divided by -4. In this case, the result is 1 because when 17 is divided by -4, the quotient is -4 and the remainder is 1. Therefore, the value of the variable "num" is 1.
9.
What is the output of the following program?
int num = 10;
while ( num < 12 )
{
cout << num;
num++;
}
Correct Answer
B. 1011
Explanation
The program starts with the variable "num" initialized to 10. The while loop condition checks if "num" is less than 12. Since 10 is less than 12, the loop is executed. Inside the loop, the value of "num" is printed, which is 10. Then, "num" is incremented by 1. The loop continues until "num" reaches 12. Therefore, the output of the program is "1011".
10.
What is the output :
int num = 10;
while(num < 12 );
{
cout << num ;
num++;
}
Correct Answer
B. Nothing is printed on console
Explanation
The while loop condition is not properly defined. The semicolon after the while statement creates an empty loop body, which means the loop will keep executing indefinitely without any code to execute. As a result, nothing will be printed on the console.
11.
What is the output:
int num = 10;
while( num < 10 )
{
cout << num;
num++;
}
Correct Answer
D. Nothing is printed on the screen
Explanation
The condition in the while loop is "num < 10", but the initial value of "num" is already 10. Since the condition is false from the start, the code inside the loop will not be executed at all. Therefore, nothing will be printed on the screen.
12.
What is output
#include<stdio.h>
int main()
{
int a = 500, b, c;
if(a>=400)
b = 300;
c = 200;
printf("%d %d", b, c);
}
Correct Answer
B. 300 200
Explanation
The correct answer is 300 200. In this code, the variable "a" is assigned a value of 500. The if statement checks if "a" is greater than or equal to 400, which is true. Therefore, the value of "b" is assigned 300. The value of "c" is assigned 200. Finally, the printf statement prints the values of "b" and "c", which are 300 and 200 respectively.
13.
What is output of following program:
#include< stdio.h>
int main()
{
int i = 65;
char j = 'A';
if( i == j )
printf("C is WOW \n");
else
printf(" C is headache \n");
return 0;
}
Correct Answer
C. C is WOW
Explanation
The program compares the integer value of variable i (65) with the ASCII value of character j ('A'), which is also 65. Since the condition i == j is true, the program executes the code inside the if statement and prints "C is WOW" as the output.
14.
IN which line there is an error
1 #include <stdio.h>
2 int main()
3 {
4 int x = 10, y = 15; (x + 1) = 15;
5 if( x%2 == y%3)
6 printf("Carpathians\n");
7
8 }
Correct Answer
A. 4
15.
What will be the output of the following program?
#include
int main()
{
int i = 4, j = -1, k = 0, w, x, y, z;
w = i || j || k;
x = i && j && k;
y = i || j && k;
z = i && j || k;
printf(" w = %d x = %d y = %d z = %d \n" , w, x, y,z);
return 0;
}
Correct Answer
B. W = 1 x = 0 y == 1 z =1
Explanation
The program is using logical operators to assign values to variables w, x, y, and z. The logical OR operator (||) returns true (1) if at least one of the operands is true. In the case of w = i || j || k, since i is true (non-zero), the expression evaluates to true and w is assigned the value 1. The logical AND operator (&&) returns true (1) only if all of the operands are true. In the case of x = i && j && k, since j is false (0), the expression evaluates to false and x is assigned the value 0. The expression y = i || j && k is evaluated from left to right. Since i is true (non-zero), the expression i || j evaluates to true. Then, the expression true && k evaluates to the value of k. Since k is false (0), the expression evaluates to false and y is assigned the value 0. The expression z = i && j || k is evaluated from left to right. Since i is true (non-zero), the expression i && j evaluates to the value of j. Since j is false (0), the expression evaluates to false. Then, the expression false || k evaluates to the value of k. Since k is true (non-zero), the expression evaluates to true and z is assigned the value 1.
16.
What is the output:
#include< stdio.h >
int main()
{
int n =9;
( (n == 9.5) ? printf(" Correct \n ") : printf(" wrong \n") );
return 0;
}
Correct Answer
B. Wrong
Explanation
The output of the code is "wrong". This is because the expression (n == 9.5) evaluates to false since n is an integer and 9.5 is a floating-point number. Therefore, the second part of the ternary operator is executed, which is to print "wrong".