c language questions and answers




1.


1. What will be the output if you will execute following c code?

#include<stdio.h>
int main(){
int xyz=1000;
int *ptr=&xyz;
printf("%d %d",++xyz,(*(int *)xyz)--);
return 0;
}

(A) 1000 999
(B) 1001 1000
(C) 1000 1000
(D) 999 999
(E) Compilation error

Explanation:

Explanation:
printf function follows cedecl parameter passing convention. That is parameter will pass from right to left direction.
(*(int *)ptr)—-
= (*&val)—
= val—-
Here –- post decrement operator. So first it will assign its value i.e. printf function will print 1234 and value of variable val will decrement by one. So
val = 1233
Now second parameter will pass:
++val
So, val = 1234

2. 

What will be the output if you will execute following c code?

#include<stdio.h>
int main(){
int a=0;
for(a;++a;a<=100)
printf("%d",a);
    return 0;


}

(A) Print 1 to 99 infinite times 
(B) Print 1 to 99
(C) Print some garbage values
(D) Infinite loop 
(E) It will print 1 to MAX_INT then MIN_INT to -1

Explanation:

Consider on for loop:
Initial value: a
It will not affect any thing in the program.
Checking condition: ++a
It is incrementing the value of a by one in each iteration.
As we know in c zero means false. So when value of a will be zero loop will terminate.
Increment value: a <= 100
It is not incrementing the value of variable a.

So, it is clear loop will terminate when value of variable a will be zero. Hence printf function will print:


1 to 32765 then -32765 to -1 due to cyclic property of int 

3. 

What will be the output if you will execute following c code?

#include<stdio.h>
int main(){
char c[6+4]="kill me";
printf("2%s",c);
return 0;

(A) ki
(B) me
(C) kill me
(D) 2kill me 
(E) Compilation error

Explanation:

Here c is array of characters. It will store characters in the following manner:

In the printf function variable c will return memory address of first character ‘k’ i.e. 100. As we know %s will prints the stream of characters until it gets first null character (‘\0’).  


4. 

What will be the output if you will execute following c code?

#include<stdio.h>
int main() {
int i=0;
for(;i++;printf("%d",i)) ;
printf("%d",i);
return 0;
}

(A) Infinite loop 
(B) 0 1
(C)
(D) Stop after printing up to INT_MAX 
(E) Compilation error

Explanation:

Consider on loop condition which is i++
It will first return 0 then increment the value of variable i by one.
As we know in C zero represents false condition. So loop will terminate and final value of I i.e. 1 will be printed by printf function in the console window.

5. 


What will be the output if you will execute following c code?

#include<stdio.h>
int main(){
int i,j=6;
for(;i=j;j-=2)
printf("%d",j);
return 0;

(A) Compilation error 
(B) Garbage values 
(C) 642
(D) 6420
(E) Infinite loop

Explanation:

Initially:
I= Garbage
J= 6
In first iteration:
i= j = 6 i.e. condition is true.
It will print 6
J = j – 2 = 4
In second iteration:
I= j= 4 i.e. condition is true.
It will print 4
J = j – 2 = 2
In third iteration:
i= j = 2 i.e. condition is true.
It will print 2
J = j – 2 = 0
In fourth iteration:
i= j = 0 i.e. condition is false.
Hence program control will come out of the for loop.


6. 


State the correct statement.

I . In a while loop the control conditional check is performed n times.
II . In a do-while loop the control conditional check is performed n+1 times.
III . break is a keyword used with if and switch case. 

(A)
Only (I) is correct.
(B) Only (II) is correct.
(C) Only (III) is correct
(D)
Only (I) and (II) are correct.
(E) All are correct

Explanation:

We cannot use break keyword in if statement.  

7. 

What will be the output if you will execute following c code?

#include<stdio.h>
int main() {
printf("5","%f",4);
return 0;
}

(A) 54.000000 
(B) 4
(C) 5
(D) 4.000000 
(E) Compilation error 

Explanation:

Since printf function accepts variable number of arguments so if we will pass extra parameter then it will not cause of any compilation error. Printf functions always print only first string constant.


8. 
What happens when if will write the value in array subscript operator which is beyond the size of array? 
(A) Compiler gives an error message 
(B) It will return any garbage value.
(C) It will return a cyclic value.
(D) It will return zero or null value.
(E) It will cause of run time error.

Explanation:

If we will go the beyond array it will point next continuous memory address. 

9. 
The order of evaluation of operators having same precedence is decided by 
(A) Associativity rule 
(B)
Precedence rule 
(C) Left-right rule 
(D) Right-left rule 
(E) None of these

Explanation:
We apply the associativity rule only if two operators enjoy same precedence. For example: Binary plus (+) and binary minus (-) both have same precedence. So in any express its associativity will decide which operator will execute first

10. 

The set of routines stored in ROM that enable a computer to start the O.S and to communicate with the various devices in the system is known as 

(A) Device driver 
(B) Boot parameter 
(C) Bios
(D) Modem
(E) None of these

Explanation:


11. 
What will be the output if you will execute following c code?

#include<stdio.h>
struct main {
int n;
int (*p)();
};

int main() {
struct main m;
int fun();
m.n=fun();
m.p=fun;
(*(m.p))();
printf("%d",m.n);
return 0;
}
int fun() {
return printf("Hello");

(A) Hello
(B) HelloHello 
(C) HelloHello5
(D) No output 
(E) Compilation error

Explanation:
m.n=fun();
Printf function always returns an integer value which is equal to number of characters it prints. So function fun will return 5 since “Hello” has five characters.
Structure main member variable p is pointer to such function which return type is int and parameter is int.
In the statement
m.p=fun;

Pointer p is pointing to function fun. So
(*(m.p))()
=(*&fun)()
=fun()      //Since * and & always cancel to each other.

12. 


What will be the output if you will execute following c code?

#include<stdio.h>
union spc {
int x;
int a:8;
char b:3;
};
int main() {
union spc s={260};
printf("%d %d", s.a,s.b);
return 0;
}



(A) 260 0
(B) 4 -4 
(C) 4  4
(D) 260 4
(E) Compilation error

Explanation:
Size of any union is size of maximum size of its any member variable.
x: it size is two byte or 16 bit
a: It size is 8 bit
b: Its size is 3 bit
So size of union spc is max (16, 8, 3) i.e. 16 bit or 2 byte.

Binary value of 260 is: 00000001 00000100
int data type (s.x=260)is stored in the following manner:


Consider on the union member variable a:
Data bit size: 7
Sign bit size: 1
Since a = 00000100 (In binary)
Data bit: 0000100
Sign bit: 0 (Zero means positive number)
Hence its equivalent decimal value is:
(-0) *2^7 + 0*2^6 + 0*2^5 + 0*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 0*2^0 = 4
Note: ^ indicate power

Consider on the union member variable a:
Data bit size: 2
Sign bit size: 1
Since a = 100 (In binary)
Data bit: 00
Sign bit: 1 (One means negative number)
Hence its equivalent decimal value is:
(-1)* 2^2 + 0*2^1 + 0*2^0 = - 4


13. 
What will be the output if you will execute following c code?

#include<stdio.h>
int main() {
goto usa;
{
static int a=10;
usa: printf("%d",a);
}
return 0;

(A) 10
(B)
0
(C) Warning, unreachable code 
(D) Garbage value
(E) Error, unknown variable a.

Explanation:
All static variables in c are load time entity. That is static variables gets memory at the load time of the program execution. 

14. 
What will be the output if you will execute following c code?

#include<stdio.h>
int main() {
int i=3,j=2,k=1;
printf("%d / %d");
return 0;
}

(A) 1
(B) Garbage value
(C) 3/2 
(D) 1/2 
(E)
Compilation error

Explanation:
All local variables in c are stored in the stack as shown in the following figure:

As we know stack follow LIFO data structure.

15. 
The ‘continue’ keyword is used to 

(A) Continue the next iteration of a loop construct. 
(B)
Exit the block where it exists and continues after. 
(C) Exit the outermost block even if it occurs inside the innermost. 
(D) Continue the compilation even an error occurs in a program. 
(E) None of these.

Explanation:
We can use continue keyword in case of loop. When program control encounter continue keyword it stop, that iteration after that line. And transfer the program control to beginning of the loop i.e. starts a new iteration if loop condition is true.

16. 
Find the odd one out 

(A) #elif 
(B) #line 
(C) #else
(D) #ifdef 
(E) #if

Explanation:
Explanation: Except #line all preprocessor directives are syntactical part of #if - #else statement

17. 
What is the value of EOF which is declared in "stdio.h"? 

(A) 0
(B) -1
(C) Any positive value 
(D) Any negative value
(E) None of these 

Explanation:
EOF stands for end of file. It is macro constant which has been defined in the stdio.h as:
#define EOF (-1)

18. 
Which data type behaves like both integer type and character type? 
(A) short int
(B) signed int
(C) char
(D) enum
(E) None of these

Explanation:
Char data type stores ASCII value (an integer number) of any character constant.

19. 
What will be the output if you will execute following c code?

#include<stdio.h>
int main(){
struct node {
int data;
struct node *next;
};
struct node *p,*q;
p=(struct node *)malloc(sizeof(struct node));
q=(struct node *)malloc(sizeof(struct node));
p->data=10;
q->data=20;
p->next=q;
q->next=NULL;
printf("%d",p->data);
p=p->next;
printf("%d",p->data);
return 0;

(A) 10 20 
(B) 10 Garbage value 
(C) 20 Garbage value 
(D) None of these 
(E) Compilation error

Explanation:
Here p and q are pointer to structure node

20. 
What will be the output if you will execute following c code?

#include<stdio.h>
int main(){
int x=2,*y=&x;
printf("%d",x*y);
return 0;

(A) 4
(B) Garbage value
(C)
Prints result of multiplication of address of x and y
(D) 2
(E) Compilation Error

Explanation:
Illegal use of pointer.


Post a Comment