pf mid questions with answers

20
 What will be output of the following programs? 1. void main(){ float a=0.7; if(a<0.7){  printf("C"); } else{  printf("C++"); } } output: c explanation : 0.7f is float constant. Its binary value is written in 32 bit. 0.7 is double constant(default). Its binary value is written in 64 bit. 0.7L is long double constant. Its binary value is written in 80 bit.  binary value of 0.7=(0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011) now here a is a float variable while 0.7 is double constant .so a 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> 2. void main(){ int i=5,j;  j=++i+++i+++i;  printf("%d %d",i,j); }

Upload: yasir-tayyeb

Post on 08-Apr-2018

243 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 1/20

 

What will be output of the following programs?

1.  void main(){

float a=0.7;

if(a<0.7){

 printf("C");

}

else{

 printf("C++");

}

}

output: c

explanation :

0.7f is float constant. Its binary value is written in 32 bit.

0.7 is double constant(default). Its binary value is written in 64 bit.

0.7L is long double constant. Its binary value is written in 80 bit.

 binary value of 0.7=(0.1011 0011 0011 0011 0011 0011 0011 0011 0011

0011 0011)

now here a is a float variable while 0.7 is double constant .so a contain only

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

2. void main(){

int i=5,j;

 j=++i+++i+++i;

 printf("%d %d",i,j);

}

Page 2: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 2/20

output: 8 24

explanation :

rule :- ++ is preincrement operator so in any arithmetic operation it first

increment the value of variable by one in whole equation then start

assingning the end value of variable in the equation . j=++i + ++i + ++i;

initial value of i=5 after three times increment i=8 now final value of i i.e. 8

will assigne to each . so j=8+8+8; j=24; i=8;

3. void main(){

int a=2,b=7,c=10;

c=a==b;

 printf("%d",c);

}

output: 0explanation :

== is relation operator which will give only two value they are 0 : if a==b is

false 1 : if a==b is true now a=2 b=7 so a==b is false hence b=0

4. void main(){

int x;

x=10,20,30;

 printf("%d",x);

}

output: 10

explanation :comma operator (,) has least precedence and its associativity is from left to right so

assignment operator (=) has more precedence than comma operator .So = operator 

will be evaluated first than comma operator x = 10 , 20 , 30 precedence 1 2 3 first x

=10 will be evaluated then control will transfer to 20 then 30.

Page 3: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 3/20

 

5. void main(){

int a;

a=015 + 0x71 +5;

 printf("%d",a);

}

output: 131

explanation :

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

6. void main(){

 printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L));

}

output: 8 4 10

explanation :

3.14f is float 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.

7. void main(){

int x=100,y=20,z=5;

 printf("%d %d %d");

}

output: 5 20 100

By default x,y,z are auto type data which stores in stack in memory.Stack is

LIFO data structure. So in stack first stores 100 then 20 then 5 and program

Page 4: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 4/20

counter (2 byte) points top element i.e 5 .Default value of %d in printf is

data which is persent in stack.

So output is revere order of dclaration. So output will be 5 20 100.

note. Initialize variables are more near then uninialize variable.

8. void main(){

float a;

(int)a= 45;

 printf("%d",a);

}

Output: error 

Explanation: After using any operator on operand it always return some integer value. (int) i.e type casting operator is not exception for this. (int) a is converted in

any integer value and we cannot assign any constant value to another constant

value in c .So

(int)a =45; is equivalent to

3456=45 .( here 3456 in any garbage value of int(a)).

9. void main(){

int a=5;

float b;

 printf("%d",sizeof(++a+b));

 printf(" %d",a);

}

Output: 4 5

Explanation:

++a +b=6 + Garbage floating point number 

=Garbage floating point number 

//From the rule of automatic type conversion

Hence sizeof operator will return 4 because size of float data type in c is 4

 byte.

Page 5: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 5/20

Value of any variable doesn¶t modify inside sizeof operator. Hence value of 

variable a will remain 5.

10. void main(){

int array[3]={5};

int i;

for(i=0;i<=2;i++)

 printf("%d ",array[i]);

}

Output : 5 0 0

Explanation:

Storage class of an array which initializes the element of the array at the

time of declaration is static. Default initial value of static integer is zero.

11. void main(){

int array[2][2][3]={0,1,2,3,4,5,6,7,8,9,10,11};

 printf("%d",array[1][0][2]);

}

Output: 8

Explanation:array[1][0][2] means 1*(2*3)+0*(3)+3=9

thelement of array starting from

zero i.e. 8.

12. void call(int,int,int);

void main(){

int a=10;

call(a,a++,++a);}

void call(int x,int y,int z){

 printf("%d %d %d",x,y,z);

}

Output : 12 11 11

Page 6: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 6/20

Default parameter passing scheme of c is cdecl i.e. argument of function will pass

from right to left direction.

First ++a will pass and a=11

Then a++ will pass and a=11

Then a will pass and a=12

13. void main(){

float f=3.4e39;

 printf("%f",f);

}

Output: +INF 

Explanation:

If you will assign value beyond the range of float data type to the float

variable it will not show any compiler error. It will store infinity.

14. void main(){

float f=5.5f;

float x;

x=f%2;

 printf("%f",x);

}

Output:Compiler error 

Explanation:

Modular division is not allowed with floating number 

15. void main(){

int a=-20;

int b=-3;

Page 7: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 7/20

printf("%d",a%b);

}

Output: -2

Explanation:

Sign of resultant of modular division depends upon only the sign of first

operand.

16. void main(){

char c='0';

 printf("%d %d",sizeof(c),sizeof('0'));

}

Output: 1 2

Explanation:

Size of char data type is one byte while size of character constant is two byte.

17. void main(){

char *url="c:\tc\bin\rw.c";

 printf("%s",url);

}

Output: w.c in

Explanation:

1. \t is tab character which moves the cursor 8 space right.

2. \b is back space character which moves the cursor one space back.

3. \r is carriage return character which moves the cursor beginning of the

line.

Page 8: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 8/20

 

18. #define PRINT printf("c");printf("c++");

void main(){

float a=5.5;

if(a==5.5)

PRINT

else

 printf("Not equal");

}

Output: Compiler error 

Explanation:

First see intermediate file:

try.c 1:

try.c 2: void main(){

try.c 3: float a=5.5;try.c 4: if(a==5.5)

try.c 5: printf("c");printf("c++");

try.c 6: else

try.c 7: printf("Not equal");

try.c 8: }

try.c 9:

Page 9: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 9/20

try.c 10:

If there are more than one statement in if block then it is necessary to write

inside the { } otherwise it will show compiler error: misplaced else

19. #include<stdio.h>

voidmain(){

int x=1,y=2;

if(--x && --y)

  printf("x=%d y=%d",x,y);

else

 printf("%d %d",x,y);}

Output : 0 2

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:

Page 10: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 10/20

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.

20. #include<stdio.h>

#define L 10

voidmain(){

int money=10;

switch(money,money*2){

case L: printf("Willian");

 break;

case L*2:printf("Warren");

 break;

case L*3:printf("Carlos");

 break;

default: printf("Lawrence");

case L*4:printf("Inqvar"); break;

}

}

Output : warren

In c comma is also operator which enjoy least precedence. So if 

x = (a , b);

Then x = b Note: Case expression can be macro constant.

Page 11: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 11/20

21. #include<stdio.h>

voidmain(){

intconst X=0;

switch(5/4/3){

case X: printf("Clinton");

 break;

case X+1:printf("Gandhi");

 break;

case X+2:printf("Gates");

 break;

default: printf("Brown");

}

}

Output : clinton

22. #include<stdio.h>

voidmain(){

unsignedchar c=280;

switch(c){

 printf("Start\t");

case 280:printf("David Beckham\t");

case 24: printf("Ronaldinho\t");

default: printf("Ronaldo\t");

 printf("End");

}

}

Output : Ronaldinho Ronaldo End

280 is beyond the range of unsigned char. Its corresponding cyclic value is:

24In c switch case statement program control always move from the case

which satisfy the switch condition and end with either break keyword,

terminating} or any null character which will come first.

Page 12: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 12/20

23. #include<stdio.h>

int main(){

int i=2,j=2;

while(i+1?--i:j++)

 printf("%d",i);

return 0;

}

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. 1In 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.

24. #include<stdio.h>

int main(){

int i,j;

i=j=2,3;

while(--i&&j++)

 printf("%d %d",i,j);

return 0;

Page 13: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 13/20

}

Output : 1 3

25. #include<stdio.h>

int r();

intmain(){

for(r();r();r()) {

 printf("%d ",r());

}

return 0;

}intr(){

intstatic num=7;

return num--;

}

Output: 5 2

Explanation:

First iteration:

Loop initial value: r() = 7Loop 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.

26. #include<stdio.h>

int main(){

Page 14: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 14/20

int x=123;

int i={

 printf("c" "++")

};

for(x=0;x<=i;x++){

 printf("%x ",x);

}

return 0;

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

27. #include<stdio.h>

void main(){

int a = 320;

char *ptr;

 ptr =( char *)&a;

 printf("%d ",*ptr);

getch();

}Output : 64

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.

Binary value of 320 is 00000001 01000000 (In 16 bit)

Memory representation of int a = 320 is:

Page 15: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 15/20

 

So ptr is pointing only first 8 bit which color is green and Decimal value is 64.

28. #include<stdio.h>

#include<string.h>

void main(){

char *ptr1 = NULL;

char *ptr2 = 0;

strcpy(ptr1," c");

strcpy(ptr2,"questions");

 printf("\n%s %s",ptr1,ptr2);getch();

}

Output : null null

We cannot assign any string constant in null pointer by strcpy function.

29. #include<stdio.h>

#include<string.h>

void main(){

char far *p,*q;

 printf("%d %d",sizeof(p),sizeof(q));

getch();

Page 16: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 16/20

}

Output : 4 2

 p is far pointer which size is 4 byte.

By default q is near pointer which size is 2 byte.

30. #include<stdio.h>

void main(){

char arr[10];

arr = "world";

 printf("%s",arr);

getch();}

Output : Compilation error Lvalue required

Array name is constant pointer and we cannot assign any value in constant

data type after declaration.

31. #include<stdio.h>

void main(){

int i = 5;

int *p;

 p = &i;

 printf(" %u %u", *&p , &*p);

getch();

}

OUTPUT : 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

Page 17: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 17/20

 

32. #include<stdio.h>

int main(){

int *p1,**p2;double *q1,**q2;

clrscr();

 printf("%d %d ",sizeof(p1),sizeof(p2));

 printf("%d %d",sizeof(q1),sizeof(q2));

getch();

return 0;

}

Output : 2 2 2 2

Explanation:

Size of any type of pointer is 2 byte (In case of near pointer)

33. #include<stdio.h>

int main(){

int a[2][4]={3,6,9,12,15,18,21,24};

 printf("%d %d %d",*(a[1]+2),*(*(a+1)+2),2[1[a]]);return 0;

}

Output : 21 21 21

explanation

In c,

a [1][2]

=*(a [1] +2)

=*(*(a+1) +2)

=2[a [1]]

=2[1[a]]

 Now, a [1] [2] means 1*(4) +2=6th element of an array staring from zero i.e.

21.

Page 18: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 18/20

34. #include<stdio.h>

int main(){

constint x=25;

int * const p=&x;

*p=2*x;

 printf("%d",x);

return 0;

}

Output : 50

Explanation:

const keyword in c doesn¶t make any variable as constant but it only makes

the variable as read only. With the help of pointer we can modify the const

variable. In this example pointer p is pointing to address of variable x. In the

following line:

int * const p=&x;

 p is constant pointer while content of p i.e. *p is not constant.

*p=2*x put the value 50 at the memory location of variable x.

35. #include<stdio.h>

voidmain(){

char arr[7]="Network";

 printf("%s",arr);

}

OUTPUT : Garbage value

Explanation:Size of a character array should one greater than total number of characters in any

string which it stores. In c every string has one terminating null character. This

represents end of the string.

Page 19: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 19/20

 

So in the string ³Network´ , there are 8 characters and they are

µN¶,¶e¶,¶t¶,¶w¶,¶o¶,¶r¶,¶k¶ and µ\0¶. Size of array arr is seven. So array arr will store

only first sevent characters and it will note store null character.

As we know %s in prinf statement prints stream of characters until it doesn¶t get

first null character. Since array arr has not stored any null character so it will print

garbage value.

36. #include<stdio.h>voidmain(){

char arr[11]="The African Queen";

 printf("%s",arr);

}Output : Compilation error 

Explanation:

Size of any character array cannot be less than the number of characters in any

string which it has assigned. Size of an array can be equal (excluding null

character) or greater than but never less than.

37. void main()

{

struct india

{char c;

float d;

};

struct world

{

int a[3];

Page 20: PF MID Questions With Answers

8/6/2019 PF MID Questions With Answers

http://slidepdf.com/reader/full/pf-mid-questions-with-answers 20/20

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();

}Output: 2 p q 1.400000

38. #include "string.h"

voidmain(){clrscr();

 printf("%d %d",sizeof("string"),strlen("string"));

getch();

}

output: 7 6

Explanation:

Sizeof operator returns the size of string including null character while strlen

function returns length of a string excluding null character.