SP2 Contest 1
Question 1 |
Predict the output of below C program:
#include<stdio.h> int main() { int a = 5, b = 10%9, i; for(i=1; i<10; i++) if(a != b); printf("a = %d b = %dn", a, b); return 0; }
5 1
| |
Prints “a = 5 b = 1” ten times. | |
a = 5 b = 1
| |
The program will not produce any output
|
Discuss it
Question 1 Explanation:
The printf statement is outside the if block because of the semicolon operator after the if statement and thus it is also outside the for loop block. So, it is executed only once.
Question 2 |
Predict the output of the below C program:
C
#include <stdio.h> int main() { int i = 2, j = -2; if((printf("%d", j)) < (printf("%d", i))) printf("%d", i); else printf("%d", j); return 0; }
2-22 | |
-22-2 | |
222 | |
Compilation Error |
Discuss it
Question 2 Explanation:
We know that printf() returns the number of character it prints. Hence printf(“%d”,j) will return -2 and newline i.e. 3 characters, and printf(“%d”, i) will return 2 and newline i.e. 2. Thus if statement will look like (3 > 2) which is false. So the else block is executed followed by the execution of printf() statements within the if condition.
Question 3 |
What will be the output of the below C program?
#include <stdio.h> int main() { int a = 2; if("%d=gfg", a); return 0; }
Compilation Error | |
2=gfg | |
No Output | |
2 |
Discuss it
Question 3 Explanation:
There is no printf() statement in the above program, so the program will not print any output. Also, the program is syntactically error-free so there is no compilation error.
Question 4 |
What will be the output of the below C program?
#include <stdio.h> int main() { int x = 2; do{ ++x; printf("%d", x); } while(--x <= 2); return 0; }
Runtime Error | |
2 | |
3 | |
Infinite Loop |
Discuss it
Question 4 Explanation:
As the value of x is incremented(++x) and then decremented(--x), hence no change in the value of x will occur and it will be as 2.
Question 5 |
What will be the output of below C++ program?
#include <iostream> using namespace std; int main() { float _ = 20.5; int __ = 20; cout<< __ + _ <<endl; return 0; }
Runtime Error | |
Compile Time Error | |
__+_ | |
40.5 |
Discuss it
Question 5 Explanation:
Underscore(_) and double underscores(__) can be used as variable names.
Question 6 |
Predict the output of the below C program:
#include <stdio.h> int main() { int a = - -3; printf("a=%d", a); return 0; }
a=3 | |
a=-3 | |
a=2 | |
None of the above |
Discuss it
Question 6 Explanation:
Notice the space between the two minus(-) operators. Here unary operation minus(-) is applied twice instead of the pre-decrement operator, we know in maths that minus*minus gives plus. Therefore, - -3 = -(-3) = 3.
Question 7 |
Predict the output of the below C program:
#include <stdio.h> int main() { int a = 5, b, c = 15, d = 13; b = (a = c, c += a, d = a + c + d); printf("%d %d %d %d", a,c,b,d); return 0; }
5 30 0 58 | |
15 30 58 58 | |
15 30 0 58 | |
5 30 58 33 |
Discuss it
Question 7 Explanation:
The variable b is assigned the value of the rightmost expression that is (d = a+b+c) = 58.
Question 8 |
Which of the following statement(s) is/are true about Associativity and Precedence of operators in C.
- Associativity is only used when there are two or more operators of the same precedence.
- All operators with the same precedence have the same associativity.
- Precedence and associativity of postfix ++ and prefix ++ are same.
- Comma operator has the highest precedence among all operators.
Only 1 | |
Only 3 | |
Both 1 and 2 | |
Both 2 and 3 |
Discuss it
Question 8 Explanation:
Question 9 |
What will be the output of the below C program?
#include <stdio.h> int main() { if (sizeof(int) > -10) printf("YES"); else printf("NO"); return 0; }
YES | |
NO | |
YESNO | |
Compilation Error |
Discuss it
Question 9 Explanation:
In C, when an integer value is compared with an unsigned int, the int is promoted to unsigned. Negative numbers are stored in 2's complement form and unsigned value of the 2's complement form is much higher than the sizeof int.
Question 10 |
Which of the following statement(s) is/are True about Data-Types in C?
- If no data type is given to a variable, then the compiler automatically convert it to int data type.
- Signed is the default modifier for char and int data types.
- We can use any modifiers in the float data type.
- We can use any modifiers in double data type.
Only 1 | |
Only 3 | |
Both 1 and 2 | |
All of the above |
Discuss it
Question 10 Explanation:
There are 50 questions to complete.