Programming C/C++

DU

Structured Programming:iit

  1. Programming Output
  2. Codes
  3. Tricky Programskkkk

 


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

#include<stdio.h>
int main(){
int i=320;
char *ptr=(char *)&i;
printf(“%d”,*ptr);
return 0;
}

(A) 320
(B) 1
(C) 64
(D) Compiler error
(E) None of above


2.What will be output if you will compile and execute the following c code?

#include<stdio.h>
#define x 5+2
int main(){
int i;
i=x*x*x;
printf(“%d”,i);
return 0;
}

(A) 343
(B) 27
(C) 133
(D) Compiler error
(E) None of above

Hints: i=5+2*5+2*5+2;


3. What will be output if you will compile and execute the following c code?

#include<stdio.h>
int main(){
char c=125;
c=c+10;
printf(“%d”,c);
return 0;
}

(A) 135
(B) +INF
(C) -121
(D) -8
(E) Compiler error


4.What will be output if you will compile and execute the following c code?

#include<stdio.h>
int main(){
float a=5.2;
if(a==5.2)
printf(“Equal”);
else if(a<5.2)
printf(“Less than”);
else
printf(“Greater than”);
return 0;
}

(A) Equal
(B) Less than
(C) Greater than
(D) Compiler error
(E) None of above


5. What will be output if you will compile and execute the following c code?

#include<stdio.h>
int main(){
int i=4,x;
x=++i + ++i + ++i;
printf(“%d”,x);
return 0;
}

(A) 21
(B) 18
(C) 12
(D) Compiler error
(E) None of above


6.What will be output if you will compile and execute the following c code?

#include<stdio.h>
int main(){
int a=2;
if(a==2){
a=~a+2<<1;
printf(“%d”,a);
}
else{
break;
}
return 0;
}

(A) It will print nothing.
(B) -3
(C) -2
(D) 1
(E) Compiler error


7. What will be output if you will compile and execute the following c code?

#include<stdio.h>
int main(){
int a=10;
printf(“%d %d %d”,a,a++,++a);
return 0;
}

(A) 12 11 11
(B) 12 10 10
(C) 11 11 12
(D) 10 10 12
(E) Compiler error

Note: In c printf function follows cdecl parameter passing scheme. In this scheme parameter is passed from right to left direction.


8. What will be output if you will compile and execute the following c code?

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

(A) 0 1 2
(B) 0 1 2 3
(C) 1 2 3
(D) Compiler error
(E) Infinite loop


9.What will be output if you will compile and execute the following c code?

#include<stdio.h>
int main(){
int x;
for(x=1;x<=5;x++);
printf(“%d”,x);
return 0;
}

(A) 4
(B) 5
(C) 6
(D) Compiler error
(E) None of above

Note: Body of for loop is optional. In this question for loop will execute until value of variable x became six and condition became false.


10.What will be output if you will compile and execute the following c code?

#include <stdio.h>
#include <string.h>
int main(){
char c=’\08′;
printf(“%d”,c);
return 0;
}

(A) 8
(B) ‟8‟
(C) 9
(D) null
(E) Compiler error

Note: In c any character is starting with character „\‟ represents octal number in character. As we know octal digits are: 0, 1, 2, 3, 4, 5, 6, and 7. So 8 is not an octal digit. Hence „\08‟ is invalid octal character constant.


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

#include<stdio.h>
#define call(x,y) x##y
int main(){
int x=5,y=10,xy=20;
printf(“%d”,xy+call(x,y));
return 0;
}

(A) 35
(B) 510
(C) 15
(D) 40
(E) None of above

Note: It is clear call(x, y) has replaced by xy. a##b=ab


12. What will be output if you will compile and execute the following c code?

#include<stdio.h>
int main(){
int array[]={10,20,30,40};
printf(“%d”,-2[array]);
return 0;
}

(A) -60
(B) -30
(C) 60
(D) Garbage value
(E) Compiler error

Note: array[2]=*(array+2)=*(2+array)=2[array]=30


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

#include<stdio.h>
#define max 5;
int main(){
printf(“%d”,i++);
}

(A) 5
(B) 6
(C) 7
(D) 0
(E) Compiler error


14.What will be output of the following c program?

#include<stdio.h>
int main(){
int goto=5;
printf(“%d”,goto);
return 0;
}

(A) 5
(B) **
(C) **
(D) Compilation error
(E) None of these

Note: Name of variable is invalid. goto is keyword in c.


15.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;
}

(A) 7 21
(B) 8 21
(C) 7 24
(D) 8 24
(E) Compilation error

Note: Compiler will treat this expression j = ++i+++i+++i; as i = ++i + ++i + ++i;


16.What will be output when you will execute following c code?

#include<stdio.h>
int main(){
int check=2;
switch(check){
case 1: printf(“D.W.Steyn”);
case 2: printf(” M.G.Johnson”);
case 3: printf(” Mohammad Asif”);
default: printf(” M.Muralidaran”);
}
return 0;
}

(A) M.G.Johnson
(B) M.Muralidaran
(C) M.G.Johnson Mohammad Asif M.Muralidaran
(D) Compilation error
(E) None of the above


17. 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;
}

(A)1
(B)2
(C)3
(D)4
(E)Compilation error


18.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;
}

(A) 2
(B) 3
(C) 13
(D)Infinite loop
(E) Compilation error


19.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;
}

(A) 4
(B) 5
(C) 6
(D) 7
(E)Compilation error

 


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

#include<stdio.h>
int 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”);
return 0;

Choose all that apply:
(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


21.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;
}

(A) End End End
(B) Start Start Start
(C) Start End Start
(D) Start Start Start Start
(E) Compilation error


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

#include<stdio.h>
int main(){
char arr[11]=”The African Queen”;
printf(“%s”,arr);
return 0;
}

(A) The African Queen
(B) The
(C) Queen
(D) null
(E) Compilation error


23. What will be output if you will compile and execute the following c code?

struct marks{
int p:3;
int c:3;
int m:2;
};
void main(){
struct marks s={2,-6,5};
printf(“%d %d %d”,s.p,s.c,s.m);
}

(a) 2 -6 5
(b) 2 -6 1
(c) 2 2 1
(d) Compiler error
(e) None of these

Answer: (c)


24. What will be output if you will compile and execute the following c code?

#include “string.h”
void main(){
clrscr();
printf(“%d%d”,sizeof(“string”),strlen(“string”));
getch();
}

(a) 6 6
(b) 7 7
(c) 6 7
(d) 7 6
(e) None of these

Answer: (d)


25. 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;
}

Choose all that apply:
(A) 0
(B) 10
(C) Garbage value
(D) Any memory address
(E) Error: Cannot modify const object


26. 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;
}

Choose all that apply:
(A) 11
(B) Garbage
(C) -2
(D) We cannot predict
(E) Compilation error


27. 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;
}

Choose all that apply:
(A) -4
(B) -5
(C) 10
(D) 11
(E) Error: Cannot modify constant object


28. 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;
}

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.


29. 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;
}
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


30. Which of the following is integral data type?

(A) void
(B) char
(C) float
(D) double
(E) None of these


31. Consider on following declaration:

(i) short i=10;
(ii) static i=10;
(iii) unsigned i=10;
(iv) const i=10;

Choose correct one:
(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


32.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


33. 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(“cquestionbank”);
}
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


34. 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


35. 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


36. 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


37. 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


38. 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 +++


39. 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


40.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


 

%d bloggers like this: