C Programming :: Basic Concepts

NA
SHSTTON
1
Solv. Corr.
6
Solv. In. Corr.
7
Attempted
0 M:0 S
Avg. Time

1 / 87

What is the output of the following C Program?
#include<stdio.h>
void main()
{
int i;
printf("%d",scanf("%d",&i));  // value 20 is given as input here
}

A0

B20

CCompiler Error

D1

ENone of these

Answer: Option D

Explanation:

Scanf returns number of items successfully read and not 1/0.
Here 10 is given as input which should have been scanned successfully. So number of items read is 1.

Workspace

NA
SHSTTON
4
Solv. Corr.
3
Solv. In. Corr.
7
Attempted
0 M:0 S
Avg. Time

2 / 87

What is the output of the following C Program?
#include<stdio.h>
void main(){
    int i;
    for(i=0;i<5;i++){
         int x=0;
         printf("%d",x);
         x++;
    }  
}

A1234

B1234

C0

DInfinite loop

ECompilation error

Answer: Option C

Explanation:

The for loop runs for i=0 to i<5, i.e, for 5 times.

Each time when the control enters the loop, x becomes equal to 0, and everytime 0 gets printed. Eventhough, in line 7 the value of x gets incremented by 1, but that's of no use because each time when the control enters the loop, value of x gets re-initializied to 0.

Therefore, the correct output will be 00000

Workspace

NA
SHSTTON
2
Solv. Corr.
4
Solv. In. Corr.
6
Attempted
0 M:0 S
Avg. Time

3 / 87

What is the output of the following C Program?
#include<stdio.h>
void main()
{
main();
}

ACompiler error

BStack overflow.

CNone of these

Answer: Option B

Explanation:

main function calls itself again and again. Each time the function is called its return address is stored in the call stack.
Since there is no condition to terminate the function call, the call stack overflows at runtime. So it terminates the program and results in an error.

Workspace

NA
SHSTTON
2
Solv. Corr.
5
Solv. In. Corr.
7
Attempted
0 M:0 S
Avg. Time

4 / 87

What is the output of the following C Program?
#include<stdio.h>
int main()
{
printf("\nab");  
 printf("\bsi"); 
printf("\rha");
return 0;
}

Aabsiha

Bhai

Cab

Dsi

ENone of these

Answer: Option B

Explanation:

\n - newline

\b - backspace

\r - linefeed

Workspace

NA
SHSTTON
2
Solv. Corr.
4
Solv. In. Corr.
6
Attempted
0 M:0 S
Avg. Time

5 / 87

What is the output of the following C Program?
#include<stdio.h>
void main(){
    int z;
    z=(5,3,2);
    printf("%d",z);
}

A5

B3

C2

D10

ECompilation error

Answer: Option C

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
6
Solv. Corr.
0
Solv. In. Corr.
6
Attempted
0 M:0 S
Avg. Time

6 / 87

What is the output of the following C Program?
#include<stdio.h>
void main()
{ 
clrscr();
}
clrscr();

ANo output/error

BCompilation Error

CNone of these

Answer: Option A

Explanation:

The first clrscr() occurs inside a function. So it becomes a function call. In the second clrscr(); is a function declaration (because it is not inside any function).

Workspace

NA
SHSTTON
4
Solv. Corr.
2
Solv. In. Corr.
6
Attempted
0 M:0 S
Avg. Time

7 / 87

What is the output of the following C Program?
#include<stdio.h>
void main()
{
char not;
 not=!2; 
printf("%d",not); 
}

A2

B0

C1

DCompilation error

ENone of these

Answer: Option B

Explanation:

! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints
0.

Workspace

NA
SHSTTON
4
Solv. Corr.
1
Solv. In. Corr.
5
Attempted
0 M:0 S
Avg. Time

8 / 87

What is the output of the following C Program?
#include<stdio.h>
void main()
{
int y;
scanf("%d",&y); // Given Input is 2000
if( (y%4==0 && y%100 != 0) || y%100 == 0 )
printf("%d is a leap year");
else
printf("%d is not a leap year");
}

A2000 is not a leap year

B2000 is a leap year

CCompilation Error

DNone of these

Answer: Option B

Explanation:

It is just a simple leap year program , when if condition is evaluated it returns true value. So, "2000 is a leap year" get printed.

Workspace

NA
SHSTTON
1
Solv. Corr.
4
Solv. In. Corr.
5
Attempted
0 M:0 S
Avg. Time

9 / 87

What is the output of the following C Program?
#include<stdio.h>
int main()
{ 
int c=- -2;
printf("c=%d",c);
return 0;
}

Ac=-2

Bc=2

CGarbage Value

DCompiler Error

Answer: Option B

Explanation:

Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus.
Note:
However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable.

Workspace

NA
SHSTTON
4
Solv. Corr.
2
Solv. In. Corr.
6
Attempted
0 M:0 S
Avg. Time

10 / 87

What will be the output of the below C program.
#include<stdio.h>
void main() 
{
int i=400,j=300;
printf("%d..%d");
}

AGarbage Value

B400..300

CCompilation error

DNone of these

Answer: Option A

Explanation:

printf takes the values of the first two assignments of the program. Any number of printf's may be given. All of them take only the first two values. If more number of assignments given in the program,then printf will take garbage values.

Workspace