1.
Which of the following shows the correct hierarchy of arithmetic operations in C?
Correct Answer
D. * / + –
Explanation
The correct hierarchy of arithmetic operations in C is as follows: first, multiplication (*), then division (/), then addition (+), and finally subtraction (–). This order of operations is commonly known as "PEMDAS" or "BODMAS" and is used to determine the sequence in which arithmetic operations should be performed in an expression.
2.
What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of the array?
Correct Answer
C. The program may crash if some important data gets overwritten.
Explanation
If a value is assigned to an array element whose subscript exceeds the size of the array, it means that the program is accessing memory beyond the allocated space for the array. This can result in overwriting important data that is stored in adjacent memory locations. As a result, the program may crash or produce unexpected behavior. Therefore, the correct answer is that the program may crash if some important data gets overwritten.
3.
int testarray[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
What value does testarray[2][1][0] in the sample code above contain?
Correct Answer
A. 11
Explanation
The value in testarray[2][1][0] is 11. This is because the array is a three-dimensional array with dimensions [3][2][2]. The first index represents the outermost dimension, the second index represents the middle dimension, and the third index represents the innermost dimension. In this case, testarray[2][1][0] refers to the element in the third group, second row, and first column of the array, which is 11.
4.
What will be the output of the following statements?
int i = 3;
printf(“%d%d”,i,i++);
Correct Answer
B. 43
Explanation
The output of the given statements will be 43. The printf statement uses the format specifier "%d%d" to print two integers. The first occurrence of "i" in the printf statement will print the current value of "i" which is 3. The second occurrence of "i" in the printf statement is followed by the post-increment operator "++" which increments the value of "i" to 4. However, since the post-increment operator is used, the value of "i" is incremented after it is printed. Therefore, the second occurrence of "i" in the printf statement will also print 3. Hence, the output will be "43".
5.
With what do you replace the?
long factorial (long x)
{
????
return x * factorial(x – 1);
}
Correct Answer
D. If (x <= 1) return 1;
Explanation
The correct answer is "if (x
6.
What would be the output of the following program?
#include
main()
{
char str[]=”S\065AB”;
printf(“\n%d”, sizeof(str));
}
Correct Answer
B. 6
Explanation
The program declares a character array named "str" and initializes it with the string "S\065AB". The sizeof() function is used to determine the size of the array in bytes. Since the array has 6 characters (including the null terminator), the output of the program would be 6.
7.
What will be the output?
int main()
{
char *str="includehelp";
printf("%s",str+7);
return 0;
}
Correct Answer
A. Help
Explanation
The correct answer is "help". In this program, the pointer variable "str" is pointing to the string "includehelp". When we use the expression "str+7" in the printf statement, it means that we are starting from the 7th character of the string. So, the output will be "help".
8.
What will be the output in the given input?
int main()
{
int n,i=0;
while(scanf("%d",&n)==1)
{
printf("END\n");
}
return 0;
}
Correct Answer
D. No output and END
Explanation
The given code is a simple program that reads integers from the input until it encounters an invalid input. In this case, the program uses the scanf function to read an integer into the variable n. If the scanf function successfully reads an integer, it returns 1, and the while loop continues. Inside the loop, the program prints "END" followed by a line break. However, since there is no input provided in the given code, the scanf function will not be able to read any integers, and the while loop will not execute. Therefore, there will be no output and the program will terminate.
9.
What will be the output of following program (on 32 bit compiler)?
#include
int main()
{
int x=65;
const unsigned char c=(int)x;
printf("%c\n",c);
return 0;
}
Correct Answer
C. A
Explanation
The program will output the character 'A'. The variable 'x' is assigned the value 65, which is the ASCII value for the character 'A'. The variable 'c' is then casted to an unsigned char, which means it will only store the lowest 8 bits of the value. When the variable 'c' is printed using the '%c' format specifier, it will interpret the value as a character and print 'A'.
10.
Which header file should be included to use functions like malloc() and calloc()?
Correct Answer
B. Stdlib.h
Explanation
The correct answer is stdlib.h. This header file should be included to use functions like malloc() and calloc().
11.
What is (void*)0?
Correct Answer
A. Representation of NULL pointer
Explanation
(void*)0 is a representation of the NULL pointer. In C and C++, NULL is typically defined as (void*)0, indicating a pointer that does not point to any valid memory location. This is often used to initialize pointers or to indicate that a pointer does not currently refer to any object.
12.
How many bytes are occupied by near, far and huge pointers (DOS)?
Correct Answer
A. Near=2 far=4 huge=4
Explanation
Near pointers in DOS occupy 2 bytes of memory, while far and huge pointers occupy 4 bytes of memory each. This is because near pointers are used for accessing data within the same segment, so they only need to store the offset. Far and huge pointers, on the other hand, are used for accessing data in different segments, so they need to store both the segment and the offset. Therefore, they require more memory space.
13.
What would be the equivalent pointer expression for referring the array element a[i][j][k][l]
Correct Answer
B. *(*(*(*(a+i)+j)+k)+l)
Explanation
The correct answer is `*(*(*(*(a+i)+j)+k)+l)`. This expression is used to refer to the array element `a[i][j][k][l]`. The expression `a+i` is used to access the `i`th element of the array `a`, then `*(a+i)+j` is used to access the `j`th element of the `i`th element, and so on until `l`. Finally, `*(*(*(*(a+i)+j)+k)+l)` is used to dereference the pointer and obtain the value of the desired array element.
14.
The following program reports an error on compilation.
#include
int main()
{
float i=10, *j;
void *k;
k=&i;
j=k;
printf("%f\n", *j);
return 0;
}
Correct Answer
B. False
Explanation
The program will report an error on compilation because a void pointer cannot be directly assigned to a float pointer without a typecast. In this program, the void pointer 'k' is assigned the address of the float variable 'i', but when trying to assign 'k' to the float pointer 'j', a typecast is required. Without the typecast, the program will not compile.
15.
Point out the compile time error in the program given below.
#include
int main()
{
int *x;
*x=100;
return 0;
}
Correct Answer
C. No error
Explanation
The program has a compile-time error in the line "*x=100;". This is because the pointer variable "x" has not been assigned any memory location using the "new" keyword or by pointing it to an existing variable. Therefore, when trying to assign a value to the memory location pointed by "x", it results in an invalid assignment error. However, the given answer "No error" is incorrect as there is indeed a compile-time error in the program.
16.
How many times the below loop will run?
main()
{
int i;
i=0;
do
{
--i;
printf("%d",i);
i++;
}
while(i>=0);
Correct Answer
B. Infinite
Explanation
The loop will run infinitely because the condition in the do-while loop is i>=0, and inside the loop, i is decremented and then incremented. This means that i will always remain greater than or equal to 0, and the loop will continue to execute indefinitely.
17.
What will be the output
main()
{
int i, j, *ptr, *ptr1;
i = 10;
j = 10;
ptr = &i;
ptr1 = &j;
if(ptr == ptr1)
{
printf("True");
}
else
{
printf("False");
}
}
Correct Answer
B. False
Explanation
The output of the given code will be "False". This is because the variables "i" and "j" are assigned different values (both 10) and the pointers "ptr" and "ptr1" are assigned the addresses of these variables respectively. Since the addresses of "i" and "j" are different, the condition "ptr == ptr1" will evaluate to false, and the "else" block will be executed, resulting in the output "False".
18.
Which of the statements is correct about the program?
#include
int main()
{
int i=10;
int *j=&i;
return 0;
}
Correct Answer
C. J is a pointer to an int and stores address of i
Explanation
In the given program, the variable "j" is declared as a pointer to an int using the "*" symbol. It is then assigned the address of the variable "i" using the "&" symbol. This means that "j" is a pointer to an int and it stores the address of "i".
19.
What will be the output
main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}
Correct Answer
C. I hate U
Explanation
The output will be "I hate U" because the float variable "me" and the double variable "you" are not exactly equal even though their values are both 1.1. This is due to the way floating-point numbers are stored and compared in computer systems. Therefore, the condition in the if statement will evaluate to false and the program will execute the else statement, resulting in the "I hate U" message being printed.
20.
Which of the following function calculates the square of 'x' in C?
Correct Answer
C. Pow(x, 2)
Explanation
The pow(x, 2) function calculates the square of 'x' in C. The pow function is a mathematical function in C that is used to calculate the power of a number. In this case, the pow(x, 2) function is used to calculate the square of 'x' by raising 'x' to the power of 2.