1.
Which of the following is not a valid variable name declaration in the C programming language?
Correct Answer
C. Int 3_a
Explanation
In the C programming language, variable names cannot start with a number. They must start with either a letter or an underscore. Therefore, the declaration "int 3_a" is not valid because it starts with a number.
2.
What will happen if the below program is executed?
#include
int main()
{
int main = 3;
printf("%d", main);
return 0;
}
Correct Answer
C. It will run without any error and prints 3.
Explanation
In a function a variable can take the same name as that of the function
3.
What is the output of the following C code?
#include
int main()
{
signed char chr;
chr = 128;
printf("%d\n", chr);
return 0;
}
Correct Answer
B. -128
Explanation
When we exceed the maximum limit of char, it starts taking values from the beginning i.e. -128.
4.
What is the output of the following C code?
#include
int main()
{
int i;
if(i=0,2,3)
printf("Hello ");
else
printf("World ");
printf("%d",i);
return 0;
}
Correct Answer
C. Hello 0
Explanation
The code first initializes the variable i without assigning any value to it. Then, it checks the condition (i=0,2,3). In this condition, the comma operator is used, which evaluates each expression separated by commas and returns the value of the last expression. In this case, the value of the condition is 3. Since the condition is true, the code executes the printf("Hello ") statement. After that, it prints the value of i, which is 0. Therefore, the output is "Hello 0".
5.
What is the output of the following C code?
#include <stdio.h>
int main()
{
int i=0,j=0;
while(i<5,j<10)
{
i++;
j++;
}
printf("%d %d",i,j);
return 0;
}
Correct Answer
B. 10 10
Explanation
Here, ‘,’ operator in while() will act as LOGICAL OR operator i.e. (i
6.
What will be the output of this C code?
#include <stdio.h>
int main()
{
float f1 = 0.1;
if (f1 == 0.1)
printf("equal\n");
else
printf("not equal\n");
return 0;
}
Correct Answer
B. Not equal
Explanation
Here, 0.1 is taken as double (by default) by the compiler. So. a float value is not equal to a double value.
7.
What is ‘short int’ in C programming?
Correct Answer
D. ‘short’ is qualifier, ‘int’ is basic data type.
Explanation
In C programming, 'short int' is a data type that combines the 'short' qualifier with the 'int' basic data type. The 'short' qualifier is used to reduce the range of values that can be stored in the variable, while the 'int' data type is a basic data type used to represent integer values. Therefore, 'short int' is a combination of a qualifier and a basic data type.
8.
Does this code compile without error(s)?
// no header files included
int main()
{
int k;
{
int k;
for (k = 0; k < 10; k++); // there’s a semicolon in the end!
}
return 0;
}
Correct Answer
A. Yes
Explanation
Header files are only required for inbuilt fuctions(like: printf(),scanf()).
Data-types(like:int, float etc) are already known to the compiler.
When variable ‘k’ is declared second time, it’s declared inside another block, hence no contradiction of having two variables with same name. Also, putting a ‘;’ at the end of ‘for’ loop takes it as an empty statement.
9.
What is the output of this C code?
#include
int main()
{
int i;
scanf(“%d”, &i); // inputting 071
printf(“%d”, i);
return 0;
}
Correct Answer
B. 71
Explanation
When we declare or assign an int variable starting with 0, the compiler considers it as Hexadecimal value. But when, printf() prints, it prints it in its decimal equivalent. ‘071’ in Hexadecimal = ‘57’ in decimal But if we take input by scanf() it ignore the first digit '0'.
10.
What is the output of this C code?
#include<stdio.h>
int main()
{
printf(“%d”, printf(“Hello 2018! ”));
return 0;
}
Correct Answer
D. Hello 2018! 12
Explanation
printf() function returns the number of characters it has printed.
11.
What is the output of this C code?
#include <stdio.h>
int main()
{
if(printf("foo"))
switch(printf("bar"))
while(printf("infinite"));
return 0;
}
Correct Answer
B. Foobar
Explanation
Since printf() return the no. of characters printed so, in if it would be 3 and ‘foo’ is printed. Similarly, in switch it would also be 3, due to ‘bar’. Also, switch will search for ‘case 3’ but, it doesn’t find any ‘case 3’, so it comes out of switch and hence program terminates.
12.
What is the output of this C code?
#include <stdio.h>
#define square(x) x*x
int main()
{
int a = 2;
int sqr = square(a+1);
printf(“%d”,sqr);
return 0;
}
Correct Answer
C. 5
Explanation
#define replaces the square(x) with x*x with the value of ‘x’ provided before compiling the code.
Therefore, square(a+1) would become a+1*a+1, which is 2+1*2+1 = 2+2+1 = 5
13.
What will be the value of d in the following program?
#include
int main()
{
int a = 10, b = 5, c = 5;
int d;
d = b + (c == a);
printf("%d", d);
}
Correct Answer
C. 5
Explanation
In the given program, the value of d is determined by the expression "b + (c == a)". Here, (c == a) is a comparison expression which evaluates to false because c is not equal to a. In C language, false is represented by 0 and true is represented by 1. So, the expression becomes "b + 0" which is equal to b. Therefore, the value of d will be 5, as the value of b is 5.
14.
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 3;
int l = i / -2;
int k = i % -2;
printf("%d %d\n", l, k);
return 0;
}
Correct Answer
B. -1 1
Explanation
The code initializes the variable i with the value 3. It then performs integer division between i and -2, resulting in l being assigned the value -1. It also calculates the remainder of the division, which is assigned to k. Since the remainder of dividing 3 by -2 is 1, k is assigned the value 1. The printf statement then prints the values of l and k, resulting in the output -1 1.
15.
What is the Output of this C code?
#include <stdio.h>
int main()
{
int a = 10;
if (a == a--)
printf("TRUE 1\t");
a = 10;
if (a == --a)
printf("TRUE 2\t");
}
Correct Answer
D. Compiler Dependent
Explanation
The output of this C code is Compiler Dependent. This is because the behavior of the code depends on how the compiler evaluates the expressions and the order in which the side effects of the expressions are applied. In the first if statement, the expression "a == a--" compares the value of a with the value of a before it is decremented. Some compilers may evaluate the expression as true because the value of a is copied before it is decremented, while others may evaluate it as false because the value of a is decremented before the comparison. In the second if statement, the expression "a == --a" compares the value of a with the value of a after it is decremented. Again, the behavior may vary depending on the compiler.