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;
}
A
5 1
B
Prints “a = 5 b = 1” ten times.
C
a = 5 b = 1
D
The program will not produce any output
SP2 Contest 1    
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;
}
A

2-22

B

-22-2

C

222

D

Compilation Error

SP2 Contest 1    
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;
}
A
Compilation Error
B
2=gfg
C
No Output
D
2
SP2 Contest 1    
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;
}
A
Runtime Error
B
2
C
3
D
Infinite Loop
SP2 Contest 1    
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;
}
A
Runtime Error
B
Compile Time Error
C
__+_
D
40.5
SP2 Contest 1    
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
a=3
B
a=-3
C
a=2
D
None of the above
SP2 Contest 1    
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;
}
A
5 30 0 58
B
15 30 58 58
C
15 30 0 58
D
5 30 58 33
SP2 Contest 1    
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.
  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.
A
Only 1
B
Only 3
C
Both 1 and 2
D
Both 2 and 3
SP2 Contest 1    
Discuss it


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;
}
A
YES
B
NO
C
YESNO
D
Compilation Error
SP2 Contest 1    
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?
  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.
A
Only 1
B
Only 3
C
Both 1 and 2
D
All of the above
SP2 Contest 1    
Discuss it


There are 50 questions to complete.
My Personal Notes arrow_drop_up