1. Datatypes
Note: As you know size of data types is compiler dependent in c. Answer of all question is based upon that compilers whose word size is two byte. Just to know you, size of data type in 2 bytes compilers is as follow:
int : 2 byte
float : 4 byte
double : 8 byte
Please adjust the answers according to size of data types in you compiler.
1.
What will be output when you will execute following c code?
#include<stdio.h>
int main(){
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d",sizeof('A'));
return 0;
return 0;
}
Choose all that apply:
(A) 4 2 1
(B) 8 2 1
(C) 4 4 1
(D) 8 4 1
(E) 8 4 2
Answer: E
Turbo C++ 3.0: 8 4 2
Turbo C ++4.5: 8 4 2
Linux GCC: 8 4 4
Visual C++: 8 4 4
By default data type of numeric constants is:
6.5 : double
90000: long int
‘A’: char
In C size of data type varies from compiler to compiler.
In TURBO C 3.0 (16 bit compilers) size of:
double is 8 byte
Long int is 4 byte
Character constant is 2 byte (size of char data type is one byte)
In TURBO C 4.5 or Linux GCC compilers (32 bit compilers) size of:
double is 8 byte
long int is 8 byte
Character constant is 2 byte
Consider on following declaring of enum.
(i) enum cricket {Gambhir,Smith,Sehwag}c;
(ii) enum cricket {Gambhir,Smith,Sehwag};
(iii) enum {Gambhir,Smith=-5,Sehwag}c;
(iv) enum c {Gambhir,Smith,Sehwag};
Choose correct one:
(A) Only (i) is correct declaration
(B) Only (i) and (ii) is correct declaration
(C) Only (i) and (iii) are correct declaration
(D) Only (i),(ii) and are correct declaration
(E) All four are correct declaration
Answer:E
Syntax of enum data type is:
enum [<tag_name>]{
<enum_constanat_name> [=<integer_ value>],
…
} [<var_name>,…]
Note:
[] : Represents optional .
<>: Represents any valid c identifier
3.
Choose all that apply:
What will be output when you will execute following c code?
#include<stdio.h>
int main(){
signed x;
unsigned y;
x = 10 +- 10u + 10u +- 10;
y = x;
if(x==y)
printf("%d %d",x,y);
else if(x!=y)
printf("%u %u",x,y);
return 0;
return 0;
}
(A) 0 0
(B) 65536 -10
(C) 0 65536
(D) 65536 0
(E) Compilation error
Answer: A
Explanation:
Turbo C++ 3.0: 0 0
Turbo C ++4.5: 0 0
Linux GCC: 0 0
Visual C++: 0 0
Consider on the expression:
x = 10 +- 10u + 10u +- 10;
10: It is signed integer constant.
10u: It is unsigned integer constant.
X: It is signed integer variable.
In any binary operation of dissimilar data type for example: a + b
Lower data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type.
As we know operators enjoy higher precedence than binary operators. So our expression is:
x = 10 + (-10u) + 10u + (-10);
= 10 + -10 + 10 + (-10);
= 0
Note: Signed is higher data type than unsigned int.
So, Corresponding signed value of unsigned 10u is +10
4.
Which of the following is not modifier of data type in c?
(A) extern
(B) interrupt
(C) huge
(D) register
(E) All of these are modifiers of data type
Answer:E
5.What will be output when you will execute following c code?
#include<stdio.h>
int main(){
double num=5.2;
int var=5;
printf("%d\t",sizeof(!num));
printf("%d\t",sizeof(var=15/2));
printf("%d",var);
return 0;
return 0;
}
Choose all that apply:
(A) 4 2 7
(B) 4 4 5
(C) 2 2 5
(D) 2 4 7
(E) 8 2 7
Answer:C
Explanation:
Turbo C++ 3.0: 2 2 5
Turbo C ++4.5: 2 2 5
Linux GCC: 4 4 5
Visual C++: 4 4 5
sizeof(Expr) operator always returns the an integer value which represents the size of the final value of the expression expr.
Consider on the following expression:
!num
=!5.2
=0
0 is int type integer constant and it size is 2 by in TURBO C 3.0 compiler and 4 in the TURBO C 4.5 and Linux GCC compilers.
Consider on the following expression:
var = 15/2
=> var = 7
=> 7
7 is int type integer constant.
Any expression which is evaluated inside the sizeof operator its scope always will be within the sizeof operator. So value of variable var will remain 5 in the printf statement.
6.
What will be output when you will execute following c code?
#include<stdio.h>
int main(){
const int *p;
int a=10;
p=&a;
printf("%d",*p);
return 0;
return 0;
}
Choose all that apply:
Choose all that apply:
(A) 0
(B) 10
(C) Garbage value
(D) Any memory address
(E) Error: Cannot modify const object
Answer:B
Explanation:
Turbo C++ 3.0: 10
Turbo C ++4.5: 10
Linux GCC: 10
Visual C++: 10
In the following declaration
const int *p;
p can keep address of constant integer.
7.
Choose correct one:
Consider on following declaration:
(i) short i=10;
(ii) static i=10;
(iii) unsigned i=10;
(iv) const i=10;
(A) Only (iv) is incorrect
(B) Only (ii) and (iv) are incorrect
(C) Only (ii),(iii) and (iv) are correct
(D) Only (iii) is correct
(E) All are correct declaration
Answer: E
Explanation:
Default data type of above all declaration is int.
8.
What will be output when you will execute following c code?
#include<stdio.h>
int main(){
int a= sizeof(signed) +sizeof(unsigned);
int b=sizeof(const)+sizeof(volatile);
printf("%d",a+++b);
return 0;
return 0;
}
Choose all that apply:
(A) 10
(B) 9
(C) 8
(D) Error: Cannot find size of modifiers
(E) Error: Undefined operator +++
Explanation:
Turbo C++ 3.0: 8
Turbo C ++4.5: 8
Linux GCC: 16
Visual C++: 16
Default data type of signed, unsigned, const and volatile is int. In turbo c 3.0 size of int is two byte.
So, a = 4 and b =4
Now, a+++b
= a++ + b
= 4 + 4 //due to post increment operator.
=8
Note: In turbo c 4.5 and Linux gcc compiler size of int is 4 byte so your out will be 16
9.
What will be output when you will execute following c code?
#include<stdio.h>
int main(){
signed x,a;
unsigned y,b;
a=(signed)10u;
b=(unsigned)-10;
y = (signed)10u + (unsigned)-10;
x = y;
printf("%d %u\t",a,b);
if(x==y)
printf("%d %d",x,y);
else if(x!=y)
printf("%u %u",x,y);
return 0;
return 0;
}
Choose all that apply:
(A) 10 -10 0 0
(B) 10 -10 65516 -10
(C) 10 -10 10 -10
(D) 10 65526 0 0
(E) Compilation error
Answer: D
Explanation:
Turbo C++ 3.0: 10 65526 0 0
Turbo C ++4.5: 10 65526 0 0
Linux GCC: 10 4294967286 0 0
Visual C++: 10 4294967286 0 0
a=(signed)10u;
signed value of 10u is +10
so, a=10
b=(unsigned)-10;
unsigned value of -10 is :
MAX_VALUE_OF_UNSIGNED_INT – 10 + 1
In turbo c 3.0 complier max value of unsigned int is 65535
So, b = 65526
y = (signed)10u + (unsigned)-10;
= 10 + 65526 = 65536 = 0 (Since 65536 is beyond the range of unsigned int. zero is its corresponding cyclic vlaue)
X = y = 0
10.
Which of the following is integral data type?
(A) void
(B) char
(C) float
(D) double
(E) None of these
Answer:B
Explanation:
In c char is integral data type. It stores the ASCII value of any character constant.
11.
Choose all that apply:
What will be output when you will execute following c code?
#include<stdio.h>
int main(){
volatile int a=11;
printf("%d",a);
return 0;
return 0;
}
(A) 11
(B) Garbage
(C) -2
(D) We cannot predict
(E) Compilation error
Answer:D
Explanation:
Turbo C++ 3.0: We cannot predict
Turbo C ++4.5: We cannot predict
Linux GCC: We cannot predict
Visual C++: We cannot predict
We cannot predict the value of volatile variable because its value can be changed by any microprocessor interrupt.
12.
What is the range of signed int data type in that compiler in which size of int is two byte?
(A) -255 to 255
(B) -32767 to 32767
(C) -32768 to 32768
(D) -32767 to 32768
(E) -32768 to 32767
Answer:E
Explanation:
Note: Size of int is always equal to word length of micro preprocessor in which your compiler has based.
13.
What will be output when you will execute following c code?
#include<stdio.h>
const enum Alpha{
X,
Y=5,
Z
}p=10;
int main(){
enum Alpha a,b;
a= X;
b= Z;
printf("%d",a+b-p);
return 0;
return 0;
}
Choose all that apply:
(A) -4
(B) -5
(C) 10
(D) 11
(E) Error: Cannot modify constant object
Answer:A
Explanation:
Turbo C++ 3.0: -4
Turbo C ++4.5: -4
Linux GCC: -4
Visual C++: -4
Default value of enum constant X is zero and
Z = Y + 1 = 5 + 1 = 6
So, a + b – p
=0 + 6 -10 = -4
14.
What will be output when you will execute following c code?
#include<stdio.h>
int main(){
char a=250;
int expr;
expr= a+ !a + ~a + ++a;
printf("%d",expr);
return 0;
return 0;
}
Choose all that apply:
(A) 249
(B) 250
(C) 0
(D) -6
(E) Compilation error
Answer:D
Explanation:
Turbo C++ 3.0: -6
Turbo C ++4.5: -6
Linux GCC: -6
Visual C++: -6
char a = 250;
250 is beyond the range of signed char. Its corresponding cyclic value is: -6
So, a = -6
Consider on the expression:
expr= a+ !a + ~a + ++a;
Operator! , ~ and ++ have equal precedence. And it associative is right to left.
So, First ++ operator will perform the operation. So value a will -5
Now,
Expr = -5 + !-5 + ~-5 + -5
= -5 + !-5 + 4 - 5
= -5 + 0 + 4 -5
= -6
15.
Consider on order of modifiers in following declaration:
(i)char volatile register unsigned c;
(ii)volatile register unsigned char c;
(iii)register volatile unsigned char c;
(iv)unsigned char volatile register c;
(A) Only (ii) is correct declaration
(B) Only (i) is correction declaration
(C) All are incorrect
(D) All are correct but they are different
(E) All are correct and same
Answer: E
Explanation:
Order of modifier of variable in c has not any significant.
16.
What will be output when you will execute following c code?
#include<stdio.h>
int main(){
int a=-5;
unsigned int b=-5u;
if(a==b)
printf("Avatar");
else
printf("Alien");
return 0;
return 0;
}
Choose all that apply:
(A) Avatar
(B) Alien
(C) Run time error
(D) Error: Illegal assignment
(E) Error: Don’t compare signed no. with unsigned no.
Answer: A
Explanation:
Turbo C++ 3.0: Avatar
Turbo C ++4.5: Avatar
Linux GCC: Avatar
Visual C++: Avatar
int a=-5;
Here variable a is by default signed int.
unsigned int b=-5u;
Constant -5u will convert into unsigned int. Its corresponding unsigned int value will be :
65536 – 5 + 1= 65532
So, b = 65532
In any binary operation of dissimilar data type for example: a == b
Lower data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type.
In c signed int is higher data type than unsigned int. So variable b will automatically type casted into signed int.
So corresponding signed value of 65532 is -5
Hence, a==b
17.
What will be output when you will execute following c code?
#include<stdio.h>
extern enum cricket x;
int main(){
printf("%d",x);
return 0;
return 0;
}
const enum cricket{
Taylor,
Kallis=17,
Chanderpaul
}x=Taylor|Kallis&Chanderpaul;
Choose all that apply:
(A) 0
(B) 15
(C) 16
(D) 17
(E) Compilation error
Answer:C
Explanation:
Explanation:
19.
Explanation:
Turbo C++ 3.0: Compilation error
Turbo C++ 3.0: 16
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: 16
x=Taylor|Kallis&Chanderpaul
= 0 | 17 & 18
= 0 |(17 & 18)
//& operator enjoy higher precedence than |
=0 |16
=16
18.
Which of the following is not derived data type in c?
(A) Function
(B) Pointer
(C) Enumeration
(D) Array
(E) All are derived data type
Answer:C
Enum is primitive data type.
What will be output when you will execute following c code?
#include<stdio.h>
enum A{
x,y=5,
enum B{
p=10,q
}varp;
}varx;
int main(){
printf("%d %d",x,varp.q);
return 0;
return 0;
}
Choose all that apply:
(A) 0 11
(B) 5 10
(C) 4 11
(D) 0 10
(E) Compilation error
Answer:E
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Nesting of enum constant is not possible in c.
20.
Consider on following declaration in c:
(i)short const register i=10;
(ii)static volatile const int i=10;
(iii)unsigned auto long register i=10;
(iv)signed extern float i=10.0;
Choose correct one:
(A) Only (iv)is correct
(B) Only (ii) and (iv) is correct
(C) Only (i) and (ii) is correct
(D) Only (iii) correct
(E) All are correct declaration
Answer: C
Explanation:
Option (III) is in correct due to we cannot specify two storage class auto and register in the declaration of any variable.
Option (iv) is in correct due to we cannot use signed or unsigned modifiers with float data type. In c float data type by default signed and it cannot be unsigned.
Binary value of 0.7 = (0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 )
Now here variable a is a floating point variable while 0.7 is double constant. So variable a will contain only 32 bit value i.e.
Rule :- ++ is pre increment operator so in any arithmetic expression it first increment the value of variable by one in whole expression then starts assigning the final value of variable in the expression.
Since assignment operator (=) has more precedence than comma operator .So = operator will be evaluated first than comma operator. In the following expression
Explanation:
3.14f is floating point constant. Its size is 4 byte. 3.14 is double constant (default). Its size is 8 byte. 3.14L is long double constant. Its size is 10 byte. sizeof() operator always return the size of data type which is written inside the(). It is keyword.
By default x, y, z are auto type data which are stored in stack in memory. Stack is LIFO data structure. So in stack first stores 100 then 20 then 5 and program counter will point top stack i.e. 5. Default value of %d in printf is data which is present in stack. So output is revere order of declaration. So output will be 5 20 100.
(int)a = 45; is equivalent to
3456 = 45 ( Here 3456 in any garbage value of int(a)).
(1) Declaration statement.
(2) && or operator.
(3) Comma (,) operator etc.
In the following expression:
Here break point is due to declaration .It break after each increment i.e. (initial value of i=5) after first increment value 6 assign to variable i then in next increment will occur and so on.
1.
2.Operators
C programming tricky objective type operators questions and answers with explanation for written test and interview
(1)
What will be output of the following program?
#include<stdio.h>
int main(){
float a=0.7;d
if(a<0.7){
printf("C");
}
else{
printf("C++");
}
return 0;
return 0;
}
EXPLANATION
Output:
Explanation:
0.7 is double constant (Default). Its binary value is written in 64 bit.
Turbo C++ 3.0: c
Turbo C ++4.5: c
Linux GCC: c
Visual C++: c
0.7 is double constant (Default). Its binary value is written in 64 bit.
Binary value of 0.7 = (0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 )
Now here variable a is a floating point variable while 0.7 is double constant. So variable a will contain only 32 bit value i.e.
a = 0.1011 0011 0011 0011 0011 0011 0011 0011 while
0.7 = 0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011....
It is obvious a < 0.7
0.7 = 0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011....
It is obvious a < 0.7
(2)
What will be output of the following program?
#include<stdio.h>
int main(){
int i=5,j;
j=++i+++i+++i;
printf("%d %d",i,j);
return 0;
}
EXPLANATION
Output:
Explanation:
Turbo C++ 3.0: 8 24
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Rule :- ++ is pre increment operator so in any arithmetic expression it first increment the value of variable by one in whole expression then starts assigning the final value of variable in the expression.
Compiler will treat this expression j = ++i+++i+++i; as
i = ++i + ++i + ++i;
Initial value of i = 5 due to three pre increment operator final value of i=8.
Now final value of i i.e. 8 will assigned to each variable as shown in the following figure:
So, j=8+8+8
j=24 and
i=8
(3)
What will be output of the following program?
#include<stdio.h>
int main(){
int i=1;
i=2+2*i++;
printf("%d",i);
return 0;
}
EXPLANATION
Output:
Explanation:
i++ i.e. when postfix increment operator is used any expression the it first assign the its value in the expression the it increments the value of variable by one. So,
Turbo C++ 3.0: 5
Turbo C ++4.5: 5
Linux GCC: 5
Visual C++: 5
i++ i.e. when postfix increment operator is used any expression the it first assign the its value in the expression the it increments the value of variable by one. So,
i = 2 + 2 * 1
i = 4
Now i will be incremented by one so i = 4 + 1 = 5
(4)
What will be output of the following program?
#include<stdio.h>
int main(){
int a=2,b=7,c=10;
c=a==b;
printf("%d",c);
return 0;
}
EXPLANATION
Output:
Explanation:
== is relational operator which returns only two values.
Turbo C++ 3.0: 0
Turbo C ++4.5: 0
Linux GCC: 0
Visual C++: 0
== is relational operator which returns only two values.
0: If a == b is false
1: If a == b is true
Since
a=2
b=7
So, a == b is false hence b=0
(5)
What will be output of the following program?
#include<stdio.h>
void main(){
int x;
x=10,20,30;
printf("%d",x);
return 0;
}
EXPLANATION
Output:
Explanation :
Turbo C++ 3.0: 10
Turbo C ++4.5: 10
Linux GCC: 10
Visual C++: 10
Precedence table:
Operator
|
Precedence
|
Associative
|
=
|
More than ,
|
Right to left
|
,
|
Least
|
Left to right
|
Since assignment operator (=) has more precedence than comma operator .So = operator will be evaluated first than comma operator. In the following expression
x = 10, 20, 30
First 10 will be assigned to x then comma operator will be evaluated.
(6)
What will be output of the following program?
#include<stdio.h>
int main(){
int a=0,b=10;
if(a=0){
printf("true");
}
else{
printf("false");
}
return 0;
}
EXPLANATION
Output:
Explanation:
Turbo C++ 3.0: false
Turbo C ++4.5: false
Linux GCC: false
Visual C++: false
As we know =
is assignment operator not relation operator. So, a = 0 means zero will
assigned to variable a. In c zero represent false and any non-zero
number represents true.
So, if(0) means condition is always false hence else part will execute.
(7)
What will be output of the following program?
#include<stdio.h>
int main(){
int a;
a=015 + 0x71 +5;
printf("%d",a);
return 0;
}
EXPLANATION
Output:
Explanation:
015 is octal number its decimal equivalent is = 5 * 8 ^ 0 + 1 * 8 ^ 1 = 5 + 8 = 13
Turbo C++ 3.0: 131
Turbo C ++4.5: 131
Linux GCC: 131
Visual C++: 131
015 is octal number its decimal equivalent is = 5 * 8 ^ 0 + 1 * 8 ^ 1 = 5 + 8 = 13
0x71 is hexadecimal number (0x is symbol of hexadecimal) its decimal equivalent is = 1 * 16 ^ 0 + 7 * 16 ^ 1 = 1 + 112 = 113
So, a = 13 + 113 + 5 = 131
(8)
What will be output of the following program?
#include<stdio.h>
int main(){
printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L));
return 0;
}
EXPLANATION
Output:
Turbo C++ 3.0: 8 4 10
Turbo C ++4.5: 8 4 10
Linux GCC: 8 4 12
Visual C++: 8 4 8
Explanation:
3.14f is floating point constant. Its size is 4 byte. 3.14 is double constant (default). Its size is 8 byte. 3.14L is long double constant. Its size is 10 byte. sizeof() operator always return the size of data type which is written inside the(). It is keyword.
(9)
What will be output of the following program?
#include<stdio.h>
int main(){
int x=100,y=20,z=5;
printf("%d %d %d");
return 0;
}
EXPLANATION
Output:
Turbo C++ 3.0: 5 20 100
Turbo C ++4.5: 5 20 100
Linux GCC: Garbage values
Visual C++: 5 100 20
By default x, y, z are auto type data which are stored in stack in memory. Stack is LIFO data structure. So in stack first stores 100 then 20 then 5 and program counter will point top stack i.e. 5. Default value of %d in printf is data which is present in stack. So output is revere order of declaration. So output will be 5 20 100.
(10)
What will be output of the following program?
#include<stdio.h>
int main(){
int a=2;
a=a++ + ~++a;
printf("%d",a);
return 0;
}
EXPLANATION
Output:
Explanation:
Turbo C++ 3.0: -1
Turbo C ++4.5: 0
Linux GCC: 0
Visual C++: 0
Same theory as question (2) and (13).
(11)
What will be output of the following program?
#include<stdio.h>
int main(){
int a;
a=sizeof(!5.6);
printf("%d",a);
return 0;
}
EXPLANATION
Output:
Turbo C++ 3.0: 2
Turbo C ++4.5: 2
Linux GCC: 4
Visual C++: 4
Explanation:
! is negation operator it return either integer 0 or 1.
! Any operand = 0 if operand is non zero.
! Any operand = 1 if operand is zero.
So, !5.6 = 0
! Any operand = 0 if operand is non zero.
! Any operand = 1 if operand is zero.
So, !5.6 = 0
Since 0 is integer number and size of integer data type is two byte.
(12)
What will be output of the following program?
#include<stdio.h>
int main(){
float a;
(int)a= 45;
printf("%d,a);
return 0;
}
EXPLANATION
Output:
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Explanation:
After performing any operation on operand it always return some constant value.
(int) i.e.
type casting operator is not exception for this. (int) a will return one
constant value and we cannot assign any constant value to another
constant value in c.
(int)a = 45; is equivalent to
3456 = 45 ( Here 3456 in any garbage value of int(a)).
(13)
What will be output of the following program?
#include<stdio.h>
int main(){
int i=5;
int a=++i + ++i + ++i;
printf("%d",a);
return 0;
}EXPLANATION
Output:
Explanation:
Turbo C++ 3.0: 21
Turbo C ++4.5: 21
Linux GCC: 22
Visual C++: 24
Rule : ++ (in
++i) is pre increment operator so in any arithmetic expression it first
increment the value of variable by one in whole equation up to break
point then start assigning the value of variable in the equation. There
are many break point operators in. For example:
(1) Declaration statement.
(2) && or operator.
(3) Comma (,) operator etc.
In the following expression:
int a=++i + ++i + ++i;
Here break point is due to declaration .It break after each increment i.e. (initial value of i=5) after first increment value 6 assign to variable i then in next increment will occur and so on.
So, a = 6 + 7 + 8;
3.Pointers
C pointers interview questions and answers
Frequently
asked technical objective types multiple choice pointer questions with
explanation of placement in c programming language
Note: Linux GCC compilers and Visual C++ compiler doesn't support far and huge pointers.
1.
What will be output of following program?
#include<stdio.h>
int main(){
int a = 320;
char *ptr;
ptr =( char *)&a;
printf("%d ",*ptr);
return 0;
}
(A) 2
(B) 320
(C) 64
(D) Compilation error
(E) None of above
Explanation:
As we know int is two byte data byte while char is one byte data byte. char pointer can keep the address one byte at time.
Turbo C++ 3.0: 64
Turbo C ++4.5: 64
Linux GCC: 64
Visual C++: 64
As we know int is two byte data byte while char is one byte data byte. char pointer can keep the address one byte at time.
Binary value of 320 is 00000001 01000000 (In 16 bit)
Memory representation of int a = 320 is:
So ptr is pointing only first 8 bit which color is green and Decimal value is 64.
2.
What will be output of following program?
#include<stdio.h>
#include<conio.h>
int main(){
void (*p)();
int (*q)();
int (*r)();
p = clrscr;
q = getch;
r = puts;
(*p)();
(*r)("cquestionbank.blogspot.com");
(*q)();
return 0;
return 0;
}
(A) NULL
(B) cquestionbank.blogspot.com
(C) c
(D) Compilation error
(E) None of above
Explanation:
p is pointer to function whose parameter is void and return type is also void. r and q is pointer to function whose parameter is void and return type is int . So they can hold the address of such function.
Turbo C++ 3.0: cquestionbank.blogspot.com
Turbo C ++4.5: cquestionbank.blogspot.com
Linux GCC: Compilation error
Visual C++: Compilation error
p is pointer to function whose parameter is void and return type is also void. r and q is pointer to function whose parameter is void and return type is int . So they can hold the address of such function.
3.
What will be output of following program?
#include<stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j=&i;
k=&j;
printf("%u %u %d ",k,*k,**k);
return 0;
return 0;
}
(A) Address, Address, 3
(B) Address, 3, 3
(C) 3, 3, 3
(D) Compilation error
(E) None of above
Explanation:
Turbo C++ 3.0: Address, Address, 3
Turbo C ++4.5: Address, Address, Address
Linux GCC: Address, Address, 3
Visual C++: Address, Address, 3
Memory representation
Here 6024, 8085, 9091 is any arbitrary address, it may be different.
Value of k is content of k in memory which is 8085
Value of *k means content of memory location which address k keeps.
k keeps address 8085 .
Content of at memory location 8085 is 6024
In the same way **k will equal to 3.
Short cut way to calculate:
Rule: * and & always cancel to each other
i.e. *&a = a
So *k = *(&j) since k = &j
*&j = j = 6024
And
**k = **(&j) = *(*&j) = *j = *(&i) = *&i = i = 3
4.
What will be output of following program?
#include<stdio.h>
int main(){
char far *p =(char far *)0x55550005;
char far *q =(char far *)0x53332225;
*p = 80;
(*p)++;
printf("%d",*q);
return 0;
}
(A) 80
(B) 81
(C) 82
(D) Compilation error
(E) None of above
Explanation:
Turbo C++ 3.0: 81
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Far address of p and q are representing same physical address.
Physical address of 0x55550005 = (0x5555) * (0x10) + (0x0005) = 0x55555
Physical address of 0x53332225 = (0x5333 * 0x10) + (0x2225) = 0x55555
*p = 80, means content at memory location 0x55555 is assigning value 25
(*p)++ means increase the content by one at memory location 0x5555 so now content at memory location 0x55555 is 81
*q also means content at memory location 0x55555 which is 26
5.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1," c");
strcpy(ptr2,"questions");
printf("\n%s %s",ptr1,ptr2);
return 0;
}
(A) c questions
(B) c (null)
(C) (null) (null)
(D) Compilation error
(E) None of above
Explanation:
We cannot assign any string constant in null pointer by strcpy function.
Turbo C++ 3.0: (null) (null)
Turbo C ++4.5: Run time error
Linux GCC: Run time error
Visual C++: Run time error
We cannot assign any string constant in null pointer by strcpy function.
6.
What will be output of following program?
#include<stdio.h>
int main(){
int huge *a =(int huge *)0x59990005;
int huge *b =(int huge *)0x59980015;
if(a == b)
printf("power of pointer");
else
printf("power of c");
return 0;
}
(A) power of pointer
(B) power of c
(C) power of cpower of c
(D) Compilation error
(E) None of above
Explanation:
Here we are performing relational operation between two huge addresses. So first of all both a and b will normalize as:
Turbo C++ 3.0: power of pointer
Turbo C ++4.5: power of c
Linux GCC: Compilation error
Visual C++: Compilation error
Here we are performing relational operation between two huge addresses. So first of all both a and b will normalize as:
a= (0x5999)* (0x10) + (0x0005) =0x9990+0x0005=0x9995
b= (0x5998)* (0x10) + (0x0015) =0x9980+0x0015=0x9995
Here both huge addresses are representing same physical address. So a==b is true.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
register a = 25;
int far *p;
p=&a;
printf("%d ",*p);
return 0;
}
(A) power of pointer
(B) power of c
(C) power of cpower of c
(D) Compilation error
(E) None of above
Explanation:
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.
8.
p is far pointer which size is 4 byte.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
char far *p,*q;
printf("%d %d",sizeof(p),sizeof(q));
return 0;
}
(A) 2 2
(B) 4 4
(C) 4 2
(D) 2 4
(E) None of above
Explanation:
Turbo C++ 3.0: 4 4
Turbo C ++4.5: 4 4
Linux GCC: Compilation error
Visual C++: Compilation error
p is far pointer which size is 4 byte.
By default q is near pointer which size is 2 byte.
9.
What will be output of following program?
#include<stdio.h>
int main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
return 0;
}
(A) 10
(B) Address
(C) 2
(D) Compilation error
(E) None of above
Explanation:
Void pointer can hold address of any data type without type casting. Any pointer can hold void pointer without type casting.
Turbo C++ 3.0: 10
Turbo C ++4.5: 10
Linux GCC: 10
Visual C++: 10
10.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
int register a;
scanf("%d",&a);
printf("%d",a);
return 0;
}
(A) 25
(B) Address
(C) 0
(D) Compilation error
(E) None of above
Explanation:
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
11.
What will be output of following program?
#include<stdio.h>
int main(){
char arr[10];
arr = "world";
printf("%s",arr);
return 0;
}
(A) world
(B) w
(C) Null
(D) Compilation error
(E) None of above
Explanation:
Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Compilation error Lvalue required
Array name is constant pointer and we cannot assign any value in constant data type after declaration.
12.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
int a,b,c,d;
char *p = ( char *)0;
int *q = ( int *q)0;
float *r = ( float *)0;
double *s = 0;
a = (int)(p+1);
b = (int)(q+1);
c = (int)(r+1);
d = (int)(s+1);
printf("%d %d %d %d",a,b,c,d);
return 0;
return 0;
}
(A) 2 2 2 2
(B) 1 2 4 8
(C) 1 2 2 4
(D) Compilation error
(E) None of above
Explanation:
Address + 1 = next address
Turbo C++ 3.0: 1 2 4 8
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Address + 1 = next address
Since initial address of all data type is zero. So its
next address will be size of data type.
13.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
return 0;
}
(A) 1
(B) 5
(C) -5
(D) Compilation error
(E) None of above
Explanation:
Difference of two same type of pointer is always one.
Turbo C++ 3.0: 1
Turbo C ++4.5: 1
Linux GCC: 1
Visual C++: 2
Difference of two same type of pointer is always one.
14.
What will be output of following program?
#include<stdio.h>
unsigned long int (* avg())[3]{
static unsigned long int arr[3] = {1,2,3};
return &arr;
}
int main(){
unsigned long int (*ptr)[3];
ptr = avg();
printf("%d" , *(*ptr+2));
return 0;
}
(A) 1
(B) 2
(C) 3
(D) Compilation error
(E) None of above
Explanation:
Turbo C++ 3.0: 3
Turbo C ++4.5: 3
Linux GCC: 3
Visual C++: 3
15.
What will be output of following program?
#include<stdio.h>
int main(){
int * p , b;
b = sizeof(p);
printf("%d" , b);
return 0;
return 0;
}
(A) 2
(B) 4
(C) 8
(D) Compilation error
(E) None of above
Explanation:
Turbo C++ 3.0: 2 or 4
Turbo C ++4.5: 2 or 4
Linux GCC: 4
Visual C++: 4
since
in this question it has not written p is which type of pointer. So its
output will depend upon which memory model has selected. Default memory
model is small.
16.
What will be output of following program?
#include<stdio.h>
int main(){
int i = 5 , j;
int *p , *q;
p = &i;
q = &j;
j = 5;
printf("%d %d",*p,*q);
return 0;
}
(A) 5 5
(B) Address Address
(C) 5 Address
(D) Compilation error
(E) None of above
Explanation:
Turbo C++ 3.0: 5 5
Turbo C ++4.5: 5 5
Linux GCC: 5 5
Visual C++: 5 5
17.
What will be output of following program?
#include<stdio.h>
int main(){
int i = 5;
int *p;
p = &i;
printf(" %u %u", *&p , &*p);
return 0;
}
(A) 5 Address
(B) Address Address
(C) Address 5
(D) Compilation error
(E) None of above
Explanation:
Since * and & always cancel to each other.
Turbo C++ 3.0: Address Address
Turbo C ++4.5: Address Address
Linux GCC: Address Address
Visual C++: Address Address
Since * and & always cancel to each other.
i.e. *&a = a
so *&p = p which store address of integer i
&*p = &*(&i) //since p = &i
= &(*&i)
= &i
So second output is also address of i
18.
What will be output of following program?
#include<stdio.h>
int main(){
int i = 100;
printf("value of i : %d addresss of i : %u",i,&i);
i++;
printf("\nvalue of i : %d addresss of i : %u",i,&i);
return 0;
}
(A)
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
(B)
value of i : 100 addresss of i : Address
value of i : 100 addresss of i : Address
(C)
value of i : 101 addresss of i : Address
value of i : 101 addresss of i : Address
(D) Compilation error
(E) None of above
Explanation:
Turbo C++ 3.0:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Turbo C ++4.5:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Linux GCC:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Visual C++:
value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
Within
the scope of any variable, value of variable may change but its address
will never change in any modification of variable.
19.
What will be output of following program?
#include<stdio.h>
int main(){
char far *p =(char far *)0x55550005;
char far *q =(char far *)0x53332225;
*p = 25;
(*p)++;
printf("%d",*q);
return 0;
}
(A) 25
(B) Address
(C) Garbage
(D) Compilation error
(E)None of above
Explanation:
Far address of p and q are representing same physical address. Physical address of
Turbo C++ 3.0: 26
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error
Far address of p and q are representing same physical address. Physical address of
0x55550005 = 0x5555 * ox10 + ox0005 = 0x55555
Physical address of
0x53332225 = 0x5333 * 0x10 + ox2225 = 0x55555
*p = 25, means content at memory location 0x55555 is assigning value 25
(*p)++ means to increase the content by one at memory the location 0x5555 so now content of memory location at 0x55555 is 26
*q also means content at memory location 0x55555 which is 26
20.
What will be output of following program?
#include<stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j = &i;
k = &j;
printf("%u %u %u",i,j,k);
return 0;
return 0;
}
(A) 3 Address 3
(B) 3 Address Address
(C) 3 3 3
(D) Compilation error
(E) None of above
Explanation:
Looping questions and answers with explanation for written test exam and interview in c programming language
Output: 2 p q 1.400000
What will be output when you will execute following c code?
Turbo C++ 3.0: 3 Address Address
Turbo C ++4.5: 3 Address Address
Linux GCC: 3 Address Address
Visual C++: 3 Address Address
Here 6024, 8085, 9091 is any arbitrary address, it may be different.
4. Loops
Looping questions in c and answers
Looping questions and answers with explanation for written test exam and interview in c programming language
(1)
What will be output of following c code?
#include<stdio.h>
extern int x;
int main(){
do{
do{
printf("%o",x);
}
while(!-2);
}
while(0);
return 0;
}
int x=8;
EXPLANATION
Output: 10
Explanation:
Here variable
x is extern type. So it will search the definition of variable x. which
is present at the end of the code. So value of variable x =8
There are two
do-while loops in the above code. AS we know do-while executes at
least one time even that condition is false. So program control will
reach at printf statement at it will print octal number 10 which is
equal to decimal number 8.
Note: %o is used to print the number in octal format.
In inner do- while loop while condition is ! -2 = 0
In C zero
means false. Hence program control will come out of the inner do-while
loop. In outer do-while loop while condition is 0. That is again
false. So program control will also come out of the outer do-while loop.
(2)
What will be output of following c code?
#include<stdio.h>
int main(){
int i=2,j=2;
while(i+1?--i:j++)
printf("%d",i);
return 0;
}
EXPLANATION
Output: 1
Explanation:
Consider the while loop condition: i + 1 ? -- i : ++j
In first iteration:
i + 1 = 3 (True)
So ternary operator will return -–i i.e. 1
In c 1 means true so while condition is true. Hence printf statement will print 1
In second iteration:
i+ 1 = 2 (True)
So ternary operator will return -–i i.e. 0
In c zero means false so while condition is false. Hence program control will come out of the while loop.
(3)
What will be output of following c code?
#include<stdio.h>
int main(){
int x=011,i;
for(i=0;i<x;i+=3){
printf("Start ");
continue;
printf("End");
}
return 0;
}
EXPLANATION
Output: Start Start Start
Explantion:
011 is octal number. Its equivalent decimal value is 9.
So, x = 9
First iteration:
i = 0
i < x i.e. 0 < 9 i.e. if loop condition is true.
Hence printf statement will print: Start
Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be:
i += 3
i = i + 3 = 3
Second iteration:
i = 3
i < x i.e. 3 < 9 i.e. if loop condition is true.
Hence printf statement will print: Start
Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be:
i += 3
i = i + 3 = 6
Third iteration:
i = 3
i < x i.e. 6 < 9 i.e. if loop condition is true.
Hence printf statement will print: Start
Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be:
i += 3
i = i + 3 = 9
fourth iteration:
i = 6
i < x i.e. 9 < 9 i.e. if loop condition is false.
Hence program control will come out of the for loop.
(4)What will be output of following c code?
#include<stdio.h>
int main(){
int i,j;
i=j=2,3;
while(--i&&j++)
printf("%d %d",i,j);
return 0;
}
EXPLANATION
Output: 13
Explanation:
Initial value of variable
i = 2
j = 2
Consider the while condition : --i && j++
In first iteration:
--i && j++
= 1 && 2 //In c any non-zero number represents true.
= 1 (True)
So while loop condition is true. Hence printf function will print value of i = 1 and j = 3 (Due to post increment operator)
In second iteration:
--i && j++
= 0 && 3 //In c zero represents false
= 0 //False
So while loop condition is false. Hence program control will come out of the for loop.
(5)What will be output of following c code?
#include<stdio.h>
int main(){
static int i;
for(++i;++i;++i) {
printf("%d ",i);
if(i==4) break;
}
return 0;
}
EXPLANATION
Output: 24
Explanation:
Default value of static int variable in c is zero. So, initial value of variable i = 0
First iteration:
For loop starts value: ++i i.e. i = 0 + 1 = 1
For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf statement will print 2
Loop incrimination: ++I i.e. i = 2 + 1 =3
Second iteration:
For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loop condition is true. Hence printf statement will print 4.
Since is equal to for so if condition is also true. But due to break keyword program control will come out of the for loop.
(6)What will be output of following c code?
#include<stdio.h>
int main(){
int i=1;
for(i=0;i=-1;i=1) {
printf("%d ",i);
if(i!=1) break;
}
return 0;
}
EXPLANATION
Output: -1
Explanation:
Initial value of variable i is 1.
First iteration:
For loop initial value: i = 0
For loop
condition: i = -1 . Since -1 is non- zero number. So loop condition
true. Hence printf function will print value of variable i i.e. -1
Since
variable i is not equal to 1. So, if condition is true. Due to break
keyword program control will come out of the for loop.
(7)What will be output of following c code?
#include<stdio.h>
int main(){
for(;;) {
printf("%d ",10);
}
return 0;
}
EXPLANATION
Output: Infinite loop
Explanation:
In for loop each part is optional.
(8)What will be output of following c code?
#include<stdio.h>
int r();
int main(){
for(r();r();r()) {
printf("%d ",r());
}
return 0;
}
int r(){
int static num=7;
return num--;
}
EXPLANATION
Output: 5 2
Explanation:
First iteration:
Loop initial value: r() = 7
Loop condition: r() = 6
Since condition is true so printf function will print r() i.e. 5
Loop incrimination: r() = 4
Second iteration:
Loop condition: r() = 3
Since condition is true so printf function will print r() i.e. 2
Loop incrimination: r() = 1
Third iteration:
Loop condition: r() = 0
Since condition is false so program control will come out of the for loop.
(9)What will be output of following c code?
#include<stdio.h>
#define p(a,b) a##b
#define call(x) #x
int main(){
do{
int i=15,j=3;
printf("%d",p(i-+,+j));
}
while(*(call(625)+3));
return 0;
}
EXPLANATION
Output: 11
Explanation:
First iteration:
p(i-+,+j)
=i-++j // a##b
=i - ++j
=15 – 4
= 11
While condition is : *(call(625)+ 3)
= *(“625” + 3)
Note: # preprocessor operator convert the operand into the string.
=*(It will return the memory address of character ‘\0’)
= ‘\0’
= 0 //ASCII value of character null character
Since loop condition is false so program control will come out of the for loop.
(10)
#include<stdio.h>
int main(){
int i;
for(i=0;i<=5;i++);
printf("%d",i)
return 0;
}
EXPLANATION
Output: 6
Explanation:
It possible for loop without any body.
(11)What will be output of following c code?
#include<stdio.h>
int i=40;
extern int i;
int main(){
do{
printf("%d",i++);
}
while(5,4,3,2,1,0);
return 0;
}
EXPLANATION
Output: 40
Explanation:
Initial value of variable i is 40
First iteration:
printf function will print i++ i.e. 40
do - while condition is : (5,4,3,2,1,0)
Here
comma is behaving as operator and it will return 0. So while condition
is false hence program control will come out of the for loop.
(12)What will be output of following c code?
#include<stdio.h>
char _x_(int,...);
int main(){
char (*p)(int,...)=&_x_;
for(;(*p)(0,1,2,3,4); )
printf("%d",!+2);
return 0;
}
char _x_(int a,...){
static i=-1;
return i+++a;
}
EXPLANATION
Output: 0
Explanation:
In c three continuous dot represents variable number of arguments.
p is the pointer to the function _x_
First iteration of for loop:
Initial value: Nothing // In c it is optional
Loop condition: (*p)(0,1,2,3,4)
= *(&_x_)(0,1,2,3,4) // p = &_x_
= _x_(0,1,2,3,4) //* and & always cancel to each other
= return i+++a
= return i+ ++a
= return -1 + 1
= 0
Since condition is false. But printf function will print 0. It is bug of c language.
(13)What will be output of following c code?
#include<stdio.h>
int main(){
int i;
for(i=10;i<=15;i++){
while(i){
do{
printf("%d ",1);
if(i>>1)
continue;
}while(0);
break;
}
}
return 0;
}
EXPLANATION
Output: 1 1 1 1 1 1
For loop will execute six times.
Note: continue keyword in do-while loop bring the program its while condition (while(0)) which is always false.
(14)How many times this loop will execute?
#include<stdio.h>
int main(){
char c=125;
do
printf("%d ",c);
while(c++);
return 0;
}
EXPLANATION
Output: Finite times
Explanation:
If we will increment the char variable c it will increment as:
126,127,-128,-127,126 . . . . , 3, 2, 1, 0
When variable c = 0 then loop will terminate.
(15)What will be output of following c code?
#include<stdio.h>
int main(){
int x=123;
int i={
printf("c" "++")
};
for(x=0;x<=i;x++){
printf("%x ",x);
}
return 0;
}
EXPLANATION
Output: c++0 1 2 3
Explanation:
First printf function will print: c++ and return 3 to variable i.
For loop will execute three time and printf function will print 0, 1, 2 respectively. 5.Structures
Structure in c example
(q) What will be output of following c code?
void main()
{
struct employee
{
unsigned id: 8;
unsigned sex:1;
unsigned age:7;
};
struct employee emp1={203,1,23};
clrscr();
printf("%d\t%d\t%d",emp1.id,emp1.sex,emp1.age);
getch();
}
Output: 203 1 23
We can access the data member in same way.
How bit data is stored in the memory:
Minimum
size of structure which data member in bit is two byte i.e. 16 bit.
This is called word size of microprocessor. Word size depends on
microprocessor. Turbo c is based on 8086 microprocessor which word size
is two byte.
Bits are filed in from right to left direction 8 bit for id,1 bit for sex and 7 bit for age.
(q) What will be output of following c code?
void main()
{
struct bitfield
{
unsigned a:5;
unsigned c:5;
unsigned b:6;
}bit;
char *p;
struct bitfield *ptr,bit1={1,3,3};
p=&bit1;
p++;
clrscr();
printf("%d",*p);
getch();
}
Output: 12
Explanation:
Binary value of a=1 is 00001 (in 5 bit)
Binary value of b=3 is 00011 (in 5 bit)
Binary value of c=3 is 000011 (in 6 bit)
In memory it is represented as:
Let address
of bit1 is 500 which initialize to char pointer p. Since can is one byte
data type so p++ will be 501. *p means content of memory location 501
which is (00001100) and its binary equivalent is 12. Hence output is 12.
(q) What will be output of following c code?
void main()
{
struct bitfield
{
signed int a:3;
unsigned int b:13;
unsigned int c:1;
};
struct bitfield bit1={2,14,1};
clrscr();
printf("%d",sizeof(bit1));
getch();
}
Output: 4
(q) What will be output of following c code?
void main()
{
struct bitfield
{
unsigned a:3;
char b;
unsigned c:5;
int d;
}bit;
clrscr();
printf("%d",sizeof(bit));
getch();
}
Output: 5
Note:
(Actual output will 6 due to slack byte ,So Before executing this
program first go to option menu then compiler then code generation then
select word alignment then press OK)
(q) What will be output of following c code?
void main()
{
struct field
{
int a;
char b;
}bit;
struct field bit1={5,'A'};
char *p=&bit1;
*p=45;
clrscr();
printf("\n%d",bit1.a);
getch();
}
Output: 45
Nesting of structure:
Nesting
of structure is possible i.e. we can declare a structure within another
structure but it is necessary inner structure must declares structure
variable otherwise we can not access the data member of inner structure.
Example:
void main()
{
struct world
{
int a;
char b;
struct india
{
char c;
float d;
}p;
};
struct world st ={1,'A','i',1.8};
clrscr();
printf("%d\t%c\t%c\t%f",st.a,st.b,st.p.c,st.p.d);
getch();
}
Output: 1 A I 1.800000
(q) What will be output of following c code?
void main()
{
struct india
{
char c;
float d;
};
struct world
{
int a[3];
char b;
struct india orissa;
};
struct world st ={{1,2,3},'P','q',1.4};
clrscr();
printf("%d\t%c\t%c\t%f",st.a[1],st.b,st.orissa.c,st.orissa.d);
getch();
}
6.If Else
1.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=5,b=10,c=1;
if(a&&b>c){
printf("cquestionsbank2012");
}
else{
break;
}
}
Choose all that apply:
(A) cquestionbank
(B) It will print nothing
(C) Run time error
(D) Compilation error
(E) None of the above
Explanation:
Keyword break
is not syntactical part of if-else statement. So we cannot use break
keyword in if-else statement. This keyword can be use in case of loop or
switch case statement.
Hence when you will compile above code compiler will show an error message: Misplaced break.
2.
What will be output when you will execute following c code?
#define PRINT printf("Star Wars");printf(" Psycho");
#include<stdio.h>
void main(){
int x=1;
if(x--)
PRINT
else
printf("The Shawshank Redemption");
}
Choose all that apply:
(A) Stars Wars Psycho
(B) The Shawshank Redemption
(C) Warning: Condition is always true
(D) Warning: Condition is always false
(E) Compilation error
Explanation:
PRINT
is macro constant. Macro PRINT will be replaced by its defined
statement just before the actual compilation starts. Above code is
converted as:
void main(){
int x=1;
if(x--)
printf("Star Wars");
printf(" Psycho");
else
printf("The Shawshank Redemption");
}
If
you are not using opening and closing curly bracket in if clause, then
you can write only one statement in the if clause. So compiler will
think:
(i)
if(x--)
printf("Star Wars");
It is if statement without any else. It is ok.
(ii)
printf(" Psycho");
It is a function call. It is also ok
(iii)
else
printf("The Shawshank Redemption");
You cannot write else clause without any if clause. It is cause of compilation error. Hence compiler will show an error message:Misplaced else
3.
What will be output when you will execute following c code?
#define True 5==5
#include<stdio.h>
void main(){
if(.001-0.1f)
printf("David Beckham");
else if(True)
printf("Ronaldinho");
else
printf("Cristiano Ronaldo");
}
Choose all that apply:
(A) David Beckham
(B) Ronaldinho
(C) Cristiano Ronaldo
(D) Warning: Condition is always true
(E) Warning: Unreachable code
Explanation:
As we know in c zero represents false and any non-zero number represents true. So in the above code:
(0.001 – 0.1f) is not zero so it represents true. So only if clause will execute and it will print: David Beckham on console.
But it is bad programming practice to write constant as a condition in if clause. Hence compiler will show a warning message: Condition is always true
Since
condition is always true, so else clause will never execute. Program
control cannot reach at else part. So compiler will show another warning
message:
Unreachable code
4.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=100;
if(a>10)
printf("M.S. Dhoni");
else if(a>20)
printf("M.E.K Hussey");
else if(a>30)
printf("A.B. de villiers");
}
(A) M.S. Dhoni
(B) A.B. de villiers
(C) M.S Dhoni
M.E.K Hussey
A.B. de Villiers
(D) Compilation error: More than one conditions are true
(E) None of the above
Explanation:
In case of if – if else – if else … Statement if first if clause is true the compiler will never check rest of the if else clause and so on.
5.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int x=-1,y=-1;
if(++x=++y)
printf("R.T. Ponting");
else
printf("C.H. Gayle");
}
Choose all that apply:
(A) R.T Ponting
(B) C.H. Gayle
(C) Warning: x and y are assigned avalue that is never used
(D) Warning: Condition is always true
(E) Compilation error
Explanation:
Consider following statement:
++x=++y
As
we know ++ is pre increment operator in the above statement. This
operator increments the value of any integral variable by one and return
that value. After performing pre increments above statement will be:
0=0
In
C language it is illegal to assign a constant value to another
constant. Left side of = operator must be a container i.e. a variable.
So compiler will show an error message: Lvalue required
In
c if you assign any value to variable but you don’t perform any
operator or perform operation only using unary operator on the variable
the compiler will show a warning message: Variable is assigned a value that is never
6.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
if(sizeof(void))
printf("M. Muralilidaran");
else
printf("Harbhajan Singh");
}
Choose all that apply:
(A) M. Muralilidaran
(B) Harbhajan Singh
(C) Warning: Condition is always false
(D) Compilation error
(E) None of the above
Explanation:
It illegal to find size of void data type using sizeof operator. Because size of void data type is meaning less.
7.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int m=5,n=10,q=20;
if(q/n*m)
printf("William Gates");
else
printf(" Warren Buffet");
printf(" Carlos Slim Helu");
}
Choose all that apply:
(A) William Gates
(B) Warren Buffet Carlos Slim Helu
(C) Run time error
(D) Compilation error
(E) None of the above
Explanation:
Consider the following expression:
q / n * m
In this expression there are two operators. They are:
/: Division operator
*: Multiplication operator
Precedence and associate of each operator is as follow:
Precedence
|
Operator
|
Associate
|
1
|
/ , *
|
Left to right
|
Precedence
of both operators is same. Hence associate will decide which operator
will execute first. Since Associate is left to right. So / operator will
execute then * operator will execute.
= q / n * m
= 20 / 10 * 5
= 2 * 5
=10
As
we know in c zero represents false and any non-zero number represents
true. Since 10 is non- zero number so if clause will execute and print:
William Gates
Since
in else clause there is not any opening and closing curly bracket. So
compiler will treat only one statement as a else part. Hence last
statement i.e.
printf(" Carlos Slim Helu");
is not part of if-else statement. So at the end compiler will also print: Carlos Slim Helu
So output of above code will be:
William Gates Carlos Slim Helu
8.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
if(!printf("Mukesh Ambani"))
if(printf(" Lakashmi Mittal"));
}
Choose all that apply:
(A) Mukesh Ambani
(B) Lakashmi Mittal
(C) It will print nothing
(D) Mukesh Ambani Lakashmi Mittal
(E) Compilation error: if statement without body
Explanation:
Return
type of printf function is int. This function return a integral value
which is equal to number of characters a printf function will print on
console. First of all printf function will: Mukesh Ambani. Since it is
printing 13 character so it will return 13. So,
!printf("Mukesh Ambani")
= !13
= 0
In
c language zero represents false. So if(0) is false so next statement
which inside the body of first if statement will not execute.
9.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
if("ABC") printf("Barack Obama\n");
if(-1) printf("Hu Jintao\n");
if(.92L) printf("Nicolas Sarkozy\n");
if(0) printf("Ben Bernanke\n");
if('W') printf("Vladimir Putin\n");
}
Choose all that apply:
(A)
It will print nothing
(B)
Barack Obama
Hu Jintao
Nicolas Sarkozy
Vladimir Putin
(C)
Barack Obama
Hu Jintao
Nicolas Sarkozy
Ben Bernanke
Vladimir Putin
(D)
Hu Jintao
Nicolas Sarkozy
Vladimir Putin
(E)
Compilation error
Explanation:
“ABC”: It is string constant and it will always return a non-zero memory address.
0.92L: It is long double constant.
‘W’: It is character constant and its ASCII value is
As
we know in c language zero represents false and any non-zero number
represents true. In this program condition of first, second, third and
fifth if statements are true.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
if(0xA)
if(052)
if('\xeb')
if('\012')
printf("Tom hanks");
else;
else;
else;
else;
}
Choose all that apply:
(A) Tom hanks
(B) Compilation error: Misplaced else
(C) Compilation error: If without any body
(D) Compilation error: Undefined symbol
(E) Warning: Condition is always true
Explanation:
oxA: It is hexadecimal integer constant.
052: It octal integer constant.
‘\xeb’: It is hexadecimal character constant.
‘\012’: It is octal character constant.
As
we know in c zero represents false and any non-zero number represents
true. All of the above constants return a non-zero value. So all if
conditions in the above program are true.
In c it is possible to write else clause without any body.
11.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=10;
if(printf("%d",a>=10)-10)
for(;;)
break;
else;
}
Choose all that apply:
A) It will print nothing
(B) 0
(C) 1
(D) Compilation error: Misplaced else
(E) Infinite loop
Explanation:
Return
type of printf function is int. This function return a integral value
which is equal to number of charcters printf function will print on
console.
Operator
>= will return 1 if both operands are either equal or first operand
is grater than second operand. So a>=10 will return 1 since a is
equal to 10.Thus printf function will print 1. Since this function is
printing only one character so it will also return 1. So, printf("%d",a>=10) - 10
= 1 - 10
= -9
Since
-9 is non-zero number so if(-9) is true condition hence if clause will
execute which contains an infinite loop but due to break keyword it will
come out of loop.
12.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=5,b=10;
if(++a||++b)
printf("%d %d",a,b);
else
printf("John Terry");
}
Choose all that apply:
(A) 5 10
(B) 6 11
(C) 6 10
(D) 5 11
(E) John Terry
Explanation:
Consider the following expression:
++a || ++b
In this expression || is Logical OR operator. Two important properties of this operator are:
Property 1:
(Expression1) || (Expression2)
|| operator returns 0 if and only if both expressions return a zero otherwise it || operator returns 1.
Property 2:
To optimize the execution time there is rule, Expression2 will only evaluate if and only if Expression1 return zero.
In this
program initial value of a is 5. So ++a will be 6. Since ++a is
returning a non-zero so ++b will not execute and if condition will be
true and if clause will be executed.
13.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
static int i;
for(;;)
if(i+++"The Matrix")
printf("Memento");
else
break;
}
Choose all that apply:
(A) It will print Memento at one time
(B) It will print Memento at three times
(C) It will print Memento at ten times
(D) It will print Memento at infinite times
(E) Compilation error: Unknown operator +++
Explanation:
Think yourself
14.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int x=1;
if(x--)
printf("The Godfather");
--x;
else
printf("%d",x);
}
Choose all that apply:
(A) The Godfather
(B) 1
(C) 0
(D) Compilation error
(E) None of the above
Explanation:
If you are not using { and } in if clause then you can write only one statement. Otherwise it will cause of compilation error: Misplace else
15.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
if('\0');
else if(NULL)
printf("cquestionsbank2012");
else;
}
Choose all that apply:
(A) cquestionbank
(B) It will print nothing
(C) Warning: Condition is always true
(D) Warning: Unreachable code
(E) Compilation error: if statement without any body
Explanation:
‘\0’ is null character constant. Its ASCII value is zero. if(0) means false so program control will check it else if clause.
NULL is macro constant which has been defined in stdio.h which also returns zero.
16.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=5,b=10;
clrscr();
if(a<++a||b<++b)
printf("%d %d",a,b);
else
printf("John Terry");
}
Choose all that apply:
(A) 5 10
(B) 6 11
(C) 6 10
(D) Compilation error
(E) John Terry
Explanation:
Consider the following expression:
a<++a||b<++b
In
the above expression || is logical OR operator. It divides any
expression in the sub expressions. In this way we have two sub
expressions:
(1) a<++a
(2) b<++b
In the expression: a< ++a
There are two operators. There precedence and associate are:
Precedence
|
Operator
|
Associate
|
1
|
++
|
Right to left
|
2
|
<
|
Left to right
|
From table it is clear first ++ operator will perform the operation then < operator.
One
important property of pre-increment (++) operator is: In any expression
first pre-increment increments the value of variable then it assigns
same final value of the variable to all that variables. So in the
expression: a < ++a
Initial value of variable a is 5.
Step 1: Increment the value of variable a in whole expression. Final value of a is 6.
Step 2: Now start assigning value to all a in the expression. After assigning 6 expression will be:
6 < 6
Since
condition is false .So second expression i.e. b<++b will be
evaluated. Again 11 < 11 is false. So || will operator will return
zero and else clause will execute.
17.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int x=1,y=2;
if(--x && --y)
printf("x=%d y=%d",x,y);
else
printf("%d %d",x,y);
}
Choose all that apply:
(A) 1 2
(B) x=1 y=2
(C) 0 2
(D) x=0 y=1
(E) 0 1
Explanation:
Consider the following expression:
--x && --y
In this expression && is Logical AND operator. Two important properties of this operator are:
Property 1:
(Expression1) && (Expression2)
&&
operator returns 1 if and only if both expressions return a non-zero
value other wise it && operator returns 0.
Property 2:
To optimize the execution time there is rule, Expression2 will only evaluate if and only if Expression1 return a non-zero value.
In
this program initial value of x is 1. So –x will be zero. Since -–x is
returning zero so -–y will not execute and if condition will be false.
Hence else part will be executed.
18.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
signed int a=-1;
unsigned int b=-1u;
if(a==b)
printf("The Lord of the Rings");
else
printf("American Beauty");
}
Choose all that apply:
(A) The Lord of the Rings
(B) American Beauty
(C) Compilation error: Cannot compare signed number
with unsigned number
(D) Compilation error: Undefined symbol -1u
(E) Warning: Illegal operation
Explanation:
Read following tutorial:
Data type tutorial
19.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
char c=256;
char *ptr="Leon";
if(c==0)
while(!c)
if(*ptr++)
printf("%+u",c);
else
break;
}
Choose all that apply:
(A) +256+256+256+256
(B) 0000
(C) +0+0+0+0
(D) It will print +256 at infinite times
(E) Compilation error
Explanation:
In
the above program c is signed (default) char variable. Range of signed
char variable in Turbo c is from -128 to 127. But we are assigning 256
which is beyond the range of variable c. Hence variable c will store
corresponding cyclic value according to following diagram:
Since 256 is positive number move from zero in clock wise direction. You will get final value of c is zero.
if(c==0)
It is true since value of c is zero.
Negation operator i.e. ! is always return either zero or one according to following rule:
!0 = 1
!(Non-zero number) = 0
So,
!c = !0 =1
As we know in c zero represents false and any non-zero number represents true. So
while(!c) i.e. while(1) is always true.
In the above program prt is character
pointer. It is pointing to first character of string “Leon” according to
following diagram:
In the above figure value in circle represents ASCII value of corresponding character.
Initially *ptr means ‘L’. So *ptr will return ASCII value of character constant ‘L’ i.e. 76
if(*ptr++) is
equivalent to : if(‘L’) is equivalent to: if(76) . It is true so in
first iteration it will print +0. Due to ++ operation in second
iteration ptr will point to character constant ‘e’ and so on. When ptr
will point ‘\0’ i.e. null character .It will return its ASCII value i.e.
0. So if(0) is false. Hence else part will execute.
20.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=2;
if(a--,--a,a)
printf("The Dalai Lama");
else
printf("Jim Rogers");
}
Choose all that apply:
(A) The Dalai Lama
(B) Jim Rogers
(C) Run time error
(D) Compilation error: Multiple parameters in
if statement
(E) None of the above
Explanation:
Consider the following expression:
a-- , --a , a
In
c comma is behaves as separator as well as operator.In the above
expression comma is behaving as operator.Comma operator enjoy lest
precedence in precedence table andits associatively is left to right. So
first of all left most comma operator will perform operation then right
most comma will operator in the above expression.
After performing a-- : a will be 2
After performing --a : a will be 0
a=0
As we know in c zero represents false and any non-zero number represents true. Hence else part will execute.
7. Printf
Printf function questions and answer with solution
Printf objective types interview questions and answers
(1)
(1)
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=6,c=11;
clrscr();
printf("%d %d %d");
getch();
}
What will output when you compile and run the above code?
(a)Garbage value garbage value garbage value
(b)5 6 11
(c)11 6 5
(d)Compiler error
Answer: (c)
(2)
#include<stdio.h>
void main()
{
char *str="CQUESTIONSBANK2012";
clrscr();
printf(str+9);
getch();
}
What will output when you compile and run the above code?
(a)CQESTIONBANK
(b)CQUESTION
(c)BANK
(d)Compiler error
Answer: (c)
(3)
#include<stdio.h>
void main()
{
clrscr();
printf("%d",printf("CQUESTIONSBANK2012"));
getch();
}
What will output when you compile and run the above code?
(a)13CQUESTIONBANK
(b)CQUESTIONBANK13
(c)Garbage CQUESTIONBANK
(d)Compiler error
Answer: (b)
(4)
#include<stdio.h>
#include<conio.h>
void main()
{
short int a=5;
clrscr();
printf("%d"+1,a);
getch();
}
What will output when you compile and run the above code?
(a)6
(b)51
(c)d
(d)Compiler error
Answer: (c)
(5)
#include<stdio.h>
void main()
{
int i=85;
clrscr();
printf("%p %Fp",i,i);
getch();
}
What will output when you compile and run the above code?
(a)85 85
(b)0055 034E:0055
(c)0055 FFFF:0055
(d)Compiler error
Answer: (b)
(6)
#include<stdio.h>
static struct student
{
int a;
int b;
int c;
int d;
}s1={6,7,8,9},s2={4,3,2,1},s3;
void main()
{
s3=s1+s2;
clrscr();
printf("%d %d %d %d",s3.a,s3.b,s3.c,s3.d);
getch();
}
What will output when you compile and run the above code?
(a)6789
(b)4321
(c)10101010
(d)Compiler error
Answer: (d)
(7)
#include<stdio.h>
extern struct student
{
int a;
int b;
int c;
int d;
}s={6,7,8,9};
void main()
{
clrscr();
printf("%d %d %d %d",s.a,s.b,s.c,s.d);
getch();
}
What will output when you compile and run the above code?
(a)6789
(b)9876
(c)0000
(d)Compiler error
Answer: (a)
(8)
#include<stdio.h>
struct student
{
static int a;
register int b;
auto int c;
extern int d;
}s={6,7,8,9};
void main()
{
printf("%d %d % %d",s.a,s.b,s.c,s.d);
}
What will output when you compile and run the above code?
(a)6789
(b)9876
(c)0000
(d)Compiler error
Answer: (d)
(9)
#include<stdio.h>
struct student
{
int roll;
int cgpa;
int sgpa[8];
};
void main()
{
struct student s={12,8,7,2,5,9};
int *ptr;
ptr=(int *)&s;
clrscr();
printf("%d",*(ptr+3));
getch();
}
What will output when you compile and run the above code?
(a)8
(b)7
(c)2
(d)Compiler error
Answer: (c)
(10)
#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2={"anil"};
}g3={10,200};
void main()
{
struct game g1=g3;
clrscr();
printf("%d %d %s",g1.level,g1.score,g1.g2.name);
getch();
}
What will output when you compile and run the above code?
(a)10 200 anil
(b)200 10 anil
(c)10 200 null
(d)Compiler error
Answer: (d)
(11)
#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2;
}g1;
void main()
{
clrscr();
printf("%d %d %s",g1.level,g1.score,g1.g2.name);
getch();
}
What will output when you compile and run the above code?
(a)Garbage_value garbage_value garbage_value
(b)0 0 (null)
(c)Run time error
(d)Compiler error
Answer: (b)
8. Command line arguements
Command line arguments in c example
(1)What will be output of following c code?
#include<stdio.h>
int main(int count,char *argv[]){
int i=0;
for(i=0;i<count;i++)
printf("\n%s",argv[i]);
return 0;
}
//save file as arg.c
In command line
C:\tc\bin>arg c question bank
Output:
c
question
(2) What will be output of following c code?
#include<dos.h>
#include<stdio.h>
int main(){
printf("%d",_argc);
return 0;
}
//save file as countarg.c
In command line
C:\tc\bin>countarg a1 a2 b1 b2 (press enter)
Output:
5
Explanation:
Here _argc is global identifier which has defined in dos.h.it count toal number of argument in command line.
(3)Reverse any string while string is passed throw command line?
Answer:
#include<string.h>
#include<stdio.h>
int main(int count,char *str[]){
printf("%s",strrev(str[1]));
return 0;
}
(4) What will be output following c code?
#include<dos.h>
#include<stdio.h>
int main(){
int i=0;
for(i=0;i<_argc;i++)
printf("\n%s",_argv[i]);
return 0;
}
//save file as arg.c
In command line
C:\tc\bin>arg usa india japan
Output:
Usa
India
japan
Explanation:
Here
_argc,_argv is global identifier which has defined in dos.h._arg count
total number of argument in command line while _argv is array of string
which store all the argument in command line.
(5) Write a c program to create dos command type.
Answer:
#include <stdio.h>
int main(int count,char * argv[]){
int i;
FILE *ptr;
char *str;
char ch;
if(count==1){
printf("The syntax of the command is incorrect.\n");
}
for(i=1;i<cout;i++){
ptr=fopen(argv[i],"r");
if(ptr==NULL){
printf("The system cannot find the file specified.");
if(count>2)
printf("\nError occurred while procesing : %s.\n",argv[i]);
}
else{
if(count>2){
printf("%s\n\n",argv[i]);
}
while((ch=getc(ptr))!=-1)
printf("%c",ch);
}
fclose(ptr);
}
return 0;
}
Save
the above file as open.c, compile and execute the go to command mode
(current working directory) and write: open xy.c (xy.c any file present
in that directory)
To
run the open command in all directories and drive you will have to give
the path of current working directory in command mode. Write:
C:tc\bin>PATH c:\tc\bin
Now press enter key. Now your open command will work in all directory and drive.
(6) Write a c program to create dos command dir.
Answer:
#include<stdio.h>
#include<dos.h>
int main(int count,char *argv[]){
struct find_t q ;
int a;
if(count==1)
argv[1]="*.*";
a = _dos_findfirst(argv[1],1,&q);
if(a==0){
while (!a){
printf(" %s\n", q.name);
a = _dos_findnext(&q);
}
}
else{
printf("File not found");
}
return 0;
}
Save the above file as list.c, compile and execute the go to command mode (current working directory) and write: list *.c
To
run the list command in all directories and drive you will have to give
the path of current working directory in command mode. Write:
C:tc\bin>PATH c:\tc\bin
Now press enter key. Now your list command will work in all directory and drive.
Image list
(7)How can we display the entire environments vector by c program?
Answer:
#include<stdio.h>
int main(int count,char *arg[],char *argvect[]){
int i=0;
while(argvect[i]) {
printf("\n%s",argvect[i]);
i++;
}
return 0;
}
(8)Write a c program which takes a string from command line with mainfunction has no parameter and convert the string in uppercase?
Answer:
#include<dos.h>
#include<string.h>
#include<stdio.h>
int main(){
char str[15];
int i=0;
strcpy(str,_argv[1]);
for(i=0;i<=strlen(str);i++){
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nstring in uppercase : %s",str);
return 0;
}
If you have any queries or suggestions in above C Command line argument questions with solution, please share it.
No comments:
Post a Comment