1.
Find Error/Output in following code:
How many times "Placement Question" will print.
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
continue;
else
break;
printf("Placement Question");
}
return 0;
}
Correct Answer
C. 0 Times
Explanation
The code will not print "Placement Question" any times. This is because the loop condition is x
2.
Find Error/Output in following code:
int main()
{
int a = 10, b = 25;
a = b++ + a++;
b = ++b + ++a;
printf("%d %d n", a, b);
}
Correct Answer
A. 36 64
Explanation
The format specifier in the printf statement is incorrect. It should be %d %d\n instead of %d %d n. The correct specifier for a newline character is \n, not n.
The order of evaluation of the expressions involving post-increment (a++ and b++) and pre-increment (++b and ++a) is crucial. The result may vary depending on the compiler and optimization settings.
Based on the original code:
cCopy code
#include <stdio.h> int main() { int a = 10, b = 25; a = b++ + a++; b = ++b + ++a; printf("%d %d\n", a, b); return 0; }
The expected output is 36 64.
3.
Find Error/Output in following code:
void fn()
{
static int i = 10;
printf("%d ", ++i);
}
int main()
{
fn();
fn();
}
Correct Answer
C. 11 12
Explanation
The code defines a function "fn" which has a static variable "i" initialized to 10. Inside the function, the value of "i" is incremented by 1 and then printed. The main function calls the "fn" function twice.
When the "fn" function is called for the first time, the value of "i" is incremented to 11 and printed.
When the "fn" function is called for the second time, the value of "i" is incremented again to 12 and printed.
Therefore, the output will be "11 12".
4.
Find Error/Output in following code:
int main() {
int m = -10, n = 20;
n = (m < 0) ? 0 : 1;
printf("%d %d", m, n);
}
Correct Answer
A. -10 0
Explanation
The code initializes two variables, m and n, to -10 and 20 respectively. It then assigns the value of 0 to n if m is less than 0, otherwise it assigns the value of 1 to n. Finally, it prints the values of m and n, which are -10 and 0 respectively.
5.
Find Error/Output in following code:
int main() {
int x = 0, y = 0;
if(x > 0)
if(y > 0)
printf("True");
else
printf("False");
}
Correct Answer
A. No Output
Explanation
The code will not produce any output. This is because the if statement for y > 0 is nested inside the if statement for x > 0. Since x is initialized to 0, the condition x > 0 is false, and the nested if statement for y > 0 is never evaluated. Therefore, no output is printed.
6.
What is output :
#include <stdio.h>
int main()
{
printf("crazyfor\\code\n");
return 0;
}
Correct Answer
C. Crazyfor\code
Explanation
The printf statement is used to print the string "crazyfor\code\n" to the standard output. The backslash (\) is an escape character in C, and when followed by another backslash (\\), it represents a single literal backslash in the output.
7.
Output of following program?
# include <stdio.h>
void fun(int *ptr)
{
*ptr = 30;
}
int main()
{
int y = 20;
fun(&y);
printf("%d", y);
return 0;
}
Correct Answer
B. 30
Explanation
The program defines a function `fun` that takes a pointer to an integer as a parameter. Inside the function, the value at the memory address pointed to by `ptr` is changed to 30.
In the `main` function, an integer variable `y` is declared and initialized to 20. The function `fun` is called with the address of `y` as an argument. This means that the function will modify the value at the memory address of `y`, changing it to 30.
Finally, the value of `y` is printed, which is now 30. Therefore, the output of the program is 30.
8.
char *ptr;
char myString[]="abcdefg";
ptr=myString
ptr+=5;
The pointer ptr points to which string?
Correct Answer
A. Fg
Explanation
The pointer ptr points to the string "fg". This is because the pointer initially points to the start of the string "abcdefg" and then it is incremented by 5, causing it to point to the character 'f' in the string.
9.
Given float *pf; int *pi; Which of the following is true?
Correct Answer
C. Sizeof(pf) == sizeof(pi)
Explanation
The correct answer is "sizeof(pf) == sizeof(pi)". This is because the sizeof operator returns the size in bytes of its operand. In this case, both pf and pi are pointers, and the size of a pointer is typically the same regardless of the type it points to. Therefore, the sizes of pf and pi will be the same.
10.
Which of the following is not a valid C variable name?
Correct Answer
D. Int $main;
Explanation
The variable name "int $main;" is not a valid C variable name because variable names in C cannot start with a special character such as "$". Variable names in C can only start with a letter or an underscore.