SP2 Contest 1

  • Last Updated : 03 Nov, 2020

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;
}
Cross
5 1
Cross
Prints “a = 5 b = 1” ten times.
Tick
a = 5 b = 1
Cross
The program will not produce any output


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;
}
Cross

2-22

Tick

-22-2

Cross

222

Cross

Compilation Error



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;
}
Cross
Compilation Error
Cross
2=gfg
Tick
No Output
Cross
2


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;
}
Cross
Runtime Error
Cross
2
Cross
3
Tick
Infinite Loop


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;
}
Cross
Runtime Error
Cross
Compile Time Error
Cross
__+_
Tick
40.5


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;
}
Tick
a=3
Cross
a=-3
Cross
a=2
Cross
None of the above


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;
}
Cross
5 30 0 58
Tick
15 30 58 58
Cross
15 30 0 58
Cross
5 30 58 33


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.
  1. Associativity is only used when there are two or more operators of the same precedence.
  2. All operators with the same precedence have the same associativity.
  3. Precedence and associativity of postfix ++ and prefix ++ are same.
  4. Comma operator has the highest precedence among all operators.
Cross
Only 1
Cross
Only 3
Tick
Both 1 and 2
Cross
Both 2 and 3


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;
}
Cross
YES
Tick
NO
Cross
YESNO
Cross
Compilation Error


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?
  1. If no data type is given to a variable, then the compiler automatically convert it to int data type.
  2. Signed is the default modifier for char and int data types.
  3. We can use any modifiers in the float data type.
  4. We can use any modifiers in double data type.
Cross
Only 1
Cross
Only 3
Tick
Both 1 and 2
Cross
All of the above


There are 50 questions to complete.