1.
1. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int a = 10, b = 10;
if (a = 5)
b--;
printf("%d, %d", a, b--);
}
Correct Answer
C. A = 5, b = 9
Explanation
In the given code, the variable "a" is assigned a value of 10 and the variable "b" is also assigned a value of 10. The if statement checks if "a" is equal to 5, but it also assigns the value 5 to "a" instead of comparing. Therefore, the condition is true and the block of code inside the if statement is executed. As a result, the value of "a" becomes 5 and the value of "b" is decremented by 1. Finally, the printf statement prints the values of "a" and "b", which are 5 and 9 respectively.
2.
Can you combine the following two statements into one?
char *p;
p = (char*) malloc(100);
Correct Answer
C. Char *p = (char*)malloc(100);
Explanation
The correct answer is "char *p = (char*)malloc(100)". This answer combines the two statements into one by declaring a pointer variable 'p' of type char and assigning it the address returned by the malloc function, which allocates 100 bytes of memory. The typecast (char*) is used to convert the returned address to a char pointer type.
3.
3. What will be the output of the following C code?
- #include <stdio.h>
- int main()
- {
- char *str = "hello world";
- char strary[] = "hello world";
- printf("%d %d\n", sizeof(str), sizeof(strary));
- return 0;
- }
Correct Answer
C. 4 12
Explanation
The code declares a pointer variable `str` and an array `strary`, both initialized with the string "hello world". The `sizeof` operator is then used to determine the size of `str` and `strary`. Since `str` is a pointer, its size is determined by the size of a pointer variable (which is typically 4 bytes on a 32-bit system). On the other hand, `strary` is an array, so its size is determined by the number of elements multiplied by the size of each element (which is 1 byte for a `char`). Therefore, the output of the code is "4 12".
4.
What will be the output of the following C code?
- #include <stdio.h>
- int main()
- {
- int ary[2][3];
- ary[][] = {{1, 2, 3}, {4, 5, 6}};
- printf("%d\n", ary[1][0]);
- }
Correct Answer
A. Compile time error
Explanation
The given code will result in a compile-time error because the array initialization is incorrect. In C, when initializing a 2D array, we need to specify the size of the second dimension. In this case, the correct initialization would be: int ary[2][3] = {{1, 2, 3}, {4, 5, 6}}. However, in the given code, the second dimension is left empty, resulting in a compile-time error.
5.
How many times the program will print "Program" ?
#include<stdio.h>
int main()
{
printf("Program");
main();
return 0;
}
Correct Answer
D. Till stack overflows
Explanation
The program will print "Program" until the stack overflows. This is because the main function is recursively calling itself without any condition to stop the recursion. As a result, the program will continue to print "Program" indefinitely until the stack memory is exhausted and a stack overflow occurs.
6.
Which variable has the longest scope in the following C code?
- #include <stdio.h>
- int b;
- int main()
- {
- int c;
- return 0;
- }
- int a;
Correct Answer
B. B
Explanation
The variable with the longest scope in the given C code is "b". This is because "b" is declared outside of any function, making it a global variable. Global variables have a scope that extends throughout the entire program, meaning they can be accessed and modified by any function. In contrast, the variables "a" and "c" are declared within the main function, making their scope limited to that function only. Therefore, "b" has the longest scope.
7.
What is the output of the following C code if there is no error in stream fp?
- #include <stdio.h>
- int main()
- {
- FILE *fp;
- fp = fopen("newfile", "w");
- printf("%d\n", ferror(fp));
- return 0;
- }
Correct Answer
B. 0
Explanation
The code opens a file named "newfile" in write mode using the fopen function. If there is no error in opening the file, the value of fp will not be NULL. Then, the code uses the ferror function to check if there is any error in the stream fp. Since there is no error in opening the file, the ferror function will return 0. Finally, the code prints the value returned by ferror, which is 0.
8.
What will be the output of the following C code?
#include<stdio.h>
main()
{
int n;
n=f1(4);
printf("%d",n);
}
f1(int x)
{
int b;
if(x==1)
return 1;
else
b=x*f1(x-1);
return b;
}
Correct Answer
A. 24
Explanation
The given code is a recursive function that calculates the factorial of a number. In the main function, the value of n is assigned the result of calling the f1 function with the argument 4. The f1 function recursively calculates the factorial by multiplying the current number with the factorial of the previous number until x becomes 1. In this case, the factorial of 4 is calculated as 4 * 3 * 2 * 1 = 24. Therefore, the output of the code will be 24.
9.
What will be the output of the following C code?
- #include <stdio.h>
- int main()
- {
- printf("%d ", 1);
- goto l1;
- printf("%d ", 2);
- l1:goto l2;
- printf("%d ", 3);
- l2:printf("%d ", 4);
- }
Correct Answer
A. 1 4
Explanation
The output of the code will be "1 4". This is because the code uses the goto statement to jump to different parts of the code. The first printf statement prints "1" and then it jumps to the label "l1". The next printf statement is skipped because of the goto statement. Then it jumps to the label "l2" and the final printf statement prints "4". So, the output is "1 4".
10.
What will be the content of 'file.c' after executing the following program?
#include<stdio.h>
int main()
{
FILE *fp1, *fp2;
fp1=fopen("file.c", "w");
fp2=fopen("file.c", "w");
fputc('A', fp1);
fputc('B', fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
Correct Answer
A. A
Explanation
The content of 'file.c' after executing the program will be "A". This is because the program opens the file 'file.c' twice, first with mode "w" which truncates the file to zero length, and then writes the character 'A' to it using the file pointer fp1. The file is then closed. When the file is opened again with mode "w" using the file pointer fp2, it truncates the file to zero length again. Therefore, the character 'A' is the only content in the file before it is closed.
11.
If an unsigned int is 2 bytes wide then, What will be the output of the program ?
#include<stdio.h>
int main()
{
unsigned int a=0xffff;
~a;
printf("%x\n", a);
return 0;
}
Correct Answer
A. Ffff
Explanation
The program initializes an unsigned int variable 'a' with the hexadecimal value 0xffff. The '~' operator is then applied to 'a', which performs a bitwise complement operation on 'a'. This operation flips all the bits in 'a'. The program then prints the value of 'a' in hexadecimal format using the printf function. Since the bitwise complement operation flips all the bits in 'a', the output will be the complement of 0xffff, which is also 0xffff.
12.
What will be the output of the following C code?
- #include <stdio.h>
- void main()
- {
- struct student
- {
- int no;
- char name[20];
- };
- struct student s;
- no = 8;
- printf("%d", no);
- }
Correct Answer
B. Compile time error
Explanation
The code will result in a compile time error because the variable "no" is not declared or defined before it is used in the line "no = 8;". Since "no" is a member of the struct "student", it should be accessed using the dot operator with the struct variable "s". Therefore, the correct way to assign a value to "no" would be "s.no = 8;".
13.
What will be the output of the program?
#include<stdio.h>
int main()
{
int i;
i = printf("How r u\n");
i = printf("%d\n", i);
printf("%d\n", i);
return 0;
}
Correct Answer
B. How r u
8
2
Explanation
The program first prints "How r u" and returns the number of characters printed, which is 8. Then it prints the value of i, which is 8. Finally, it prints the value of i again, which is 2.
14.
What will be the output of the program (16-bit platform)?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
p = (int *)malloc(20);
printf("%d\n", sizeof(p));
free(p);
return 0;
}
Correct Answer
B. 2
Explanation
The program allocates memory of size 20 bytes using the malloc function and assigns the address of the allocated memory to the pointer variable 'p'. The sizeof(p) function returns the size of the pointer variable 'p' in bytes, which is 2 on a 16-bit platform. This is because the size of a pointer is determined by the architecture of the platform, not the size of the memory it points to. The program then frees the allocated memory using the free function and returns 0.
15.
#include<stdio.h>
#include<stdlib.h>
int main()
{
union test
{
int i;
float f;
char c;
};
union test *t;
t = (union test *)malloc(sizeof(union test));
t->f = 10.10f;
printf("%f", t->f);
return 0;
}
Correct Answer
C. 10.100000
Explanation
The correct answer is 10.100000. This is because the code declares a union called "test" which has three members: an integer, a float, and a character. It then dynamically allocates memory for a variable of type "test" using malloc(). The float member "f" is then assigned the value 10.10f. Finally, the value of "f" is printed using printf(). Since "f" was assigned the value 10.10f, the output will be 10.100000.