Вы находитесь на странице: 1из 46

1.

What will print out? main() { char *p1=name; char *p2; p2=(char*)malloc(20); memset (p2, 0, 20); while(*p2++ = *p1++); printf(%sn,p2); } Answer:empty string. What will be printed as the result of the operation below:

2.

main() { int x=20,y=35; x=y++ + x++; y= ++y + ++x; printf(%d%dn,x,y); }


Answer : 5794 What will be printed as the result of the operation below:

3.

main() { int x=5; printf(%d,%d,%dn,x,x< <2,x>>2); }


Answer: 5,20,1 What will be printed as the result of the operation below:

4.

#define swap(a,b) a=a+b;b=a-b;a=a-b; void main() { int x=5, y=10; swap (x,y); printf(%d %dn,x,y); swap2(x,y); printf(%d %dn,x,y); } int swap2(int a, int b) { int temp; temp=a; b=a;

a=temp; return 0; }
Answer: 10, 5 10, 5 What will be printed as the result of the operation below:

5.

main() { char *ptr = Cisco Systems; *ptr++; printf(%sn,ptr); ptr++; printf(%sn,ptr); }


Answer:Cisco Systems isco systems What will be printed as the result of the operation below:

6.

main() { char s1[]=Cisco; char s2[]= systems; printf(%s,s1); }


7. Answer: Cisco What will be printed as the result of the operation below:

main() { char *p1; char *p2; p1=(char *)malloc(25); p2=(char *)malloc(25); strcpy(p1,Cisco); strcpy(p2,systems); strcat(p1,p2); printf(%s,p1); }
Answer: Ciscosystems The following variable is available in file1.c, who can access it?:

8.

9. static int average;

Answer: all the functions in the file1.c can access the variable. 10. WHat will be the result of the following code?

#define TRUE 0 // some code while(TRUE) { // some code }


Answer: This will not go into the loop as TRUE is defined as 0. 11. What will be printed as the result of the operation below:

int x; int modifyvalue() { return(x+=10); } int changevalue(int x) { return(x+=1); } void main() { int x=10; x++; changevalue(x); x++; modifyvalue(); printf("First output:%dn",x); x++; changevalue(x); printf("Second output:%dn",x); modifyvalue(); printf("Third output:%dn",x); }
Answer: 12 , 13 , 13 12. What will be printed as the result of the operation below:

main() { int x=10, y=15; x = x++; y = ++y; printf(%d %dn,x,y);

}
Answer: 11, 16 13. What will be printed as the result of the operation below:

main() { int a=0; if(a==0) printf(Cisco Systemsn); printf(Cisco Systemsn); }


Answer: Two lines with Cisco Systems will be printed.

Given the following program fragment main ()

int i, j, k;

i = 3;

j =2*(i++);

k =2*(++i);

1. which one of the given option is correct? a) i = 4, j = 6. b) j = 6, k = 10.

c) i = 5, k = 6. d) j = 6, k = 8.

Answer: b

Explanation : In the expression j = 2 * (i++) the value of i is used before incrementing and in expression k =2*(++i); will get incremented first and then used in the expression

2. How many times the below loop will run

main()

int i;

i = 0;

do

--i;

printf ( %d ,i);

i++;

} while (i >= 0)

a) 1

b) Infinite

c) 0

d) Compilation Error

Answer: b

Explanation: In every iteration value of i is decremented and then incremented so remains 0 and hence a Infinite Loop.

3. switch (option) {

case H : printf( Hello );

case W : printf( Welcome );

case B : printf ( Bye );

break;

What would be the output if option = H ?

a) Hello

b) Hello Welcome

c) Hello Welcome Bye

d) None of the Above

Answer: c

Explanation : If option = H then the first case is true so Hello gets printed but there is no break statement after this case to come out of the switch statement so the program execute all other case statements also and Hello Welcome Bye get printed.

4. Suppose a,b,c are integer variables with values 5,6,7 respectively. What is the value of the expression:

!((b + c) > (a + 10))

a) 1

b) 6

c) 15

d) 0

Answer : a

Explanation:

1. !((b + c) > (a + 10))

2. !((6 + 7) > (5+10))

3. !(13 > 15) 13 is less than 15 so it will return False (0 ).

4. !(0). Not of 0 is 1.

5. Consider the following program,

main ()

int i, j;

for (i=0, j=5; j >0, i < 10; i ++, j--)

printf( \nGyantonic.com );

How many times Gyantonic.com will get printed

a) 5

b) Compilation Error

c) 10

d) None of the above

Answer: c

Explanation : Condition part of for loop ( j>0, i<10 ) is separated by commas which means that compiler will use the value which is at right hand side of comma i.e of i<10 so the loop will execute till the value of i is less than 10.

6. What will be the output of the following program?

main()

printf(3+ Gyantonic +4);

a. Compilation Error

b. ntonic

c. tonic

d ic

Answer : d

Explanation: Gyantonic is a constant string and statement

printf(3+ Gyantonic +4); will skip seven(3 + 4) characters of the string before printing.

7. What will be the output of the following program?

main()

printf( %c , Gyantonic [4]);

a. Compilation Error

b. G

c. t

d n

Answer: c

Explanation: Gyantonic is a constant string and character at index 4 will get printed.

8. What will be the output of the following program?

main()

printf( Gyantonic

\t

.com );

a. Gyantonic

.com

b.Gyantonic.com

c. Gyantonic\t.com

d. None of these

Answer: a
1.

What value of c will get printed

main() { int a, b, c; a = 10; b = 20; c = printf(%d,a) + ++b; printf (%d,c); } a. 23 c. 30 Answer: a Explanation: printf() will return no. of bytes it printed. Expression becomes c = 2 + ++b; then value of b is incremented before addition. b. 22 d. Compilation Error

2. How many times the below loop will get executed? main() { int i; for (i=9 ; i ; i=i-2) { printf(\n%d,i); } } a. 5 c. Compilation Error Answer: d Explanation : Above loop will iterate till i have non zero value. Initial value of i is 9 and it get decremented by 2 in every iteration ( 9, 7, 5, 3, 1, -1, -3 .). Value of i will never become 0 and loop will run infinitely. b. 6 d. Infinite

3. What will be the output main() { int i; i = 10; printf(%d\t,5,6);

printf(%d, i , i++); } a. 5 c. 5 11 10 b. 6 d. 6 10 11

Answer: a Explanation: Value in a function get passed from right to left. First i++ get passed and it make i = 11.

4. What the below statement will print if a=10 and b = 20? printf( %d,a==b); a. 20 c. 10 Answer: b Explanation: a==b is a expression and will return 1 (true) or 0 (False) depending on the values of a and b. Here a and b are not equal so 0 is printed. b. 0 c. 1

5. What the below statement will print if a=10? printf( %d %d,a, !a++); a. 11 c. 10 0 0 b. 10 d. 0 10 10

Answer: a

Explanation: Values in the function get passed from right to left. First !a++ get processed which pass zero as argument and make a equal to 11.

6. What will be the output main() { int i; i =10; if (i ==20 || 30) { printf(True); } else { printf(False); } } a. True c. Syntax error Answer : a Explanation: i==20 is a expression which will return TRUE or FALSE depending on the value of i. In this program it will return 0 so the statement become b. False d. Run time Error

If ( 0 || 30) 30 is a nonzero value which means TRUE (1) in C when ORed with 0 will result TRUE.

7. What will be the output of the following program? main() { printf(%c,4[Gyantonic]); } a. Compilation Error c. t Answer: c Explanation: Gyantonic is a constant string and character at index 4 will get printed. d n b. G

8. What will be the output main() { if (1,0) { printf(True); }

else { printf(False); } } a. True c. Compilation error Answer : b


1.

b. False d. Run time Error

What will be the output

main() { int i , j ,*ptr, *ptr1; i =10; j = 10; ptr = &i; ptr1 = &j;

if (ptr == ptr1) { printf(True); }

else { printf(False); } } a. True c. Syntax error Answer : b Explanation: In this program we are comparing the addresses contained by ptr & ptr1 not the value at those addresses and pointers ptr and ptr1 have the addresses of different variables so above condition is false. b. False d. Run time Error

2. How many times the below loop will get executed? main() { int i; for (i=20, i =10 ; i<=20 ; i++) { printf(\n%d,i); } } a. 1 b. Run time Error

c. 11 Answer : c

d. Compilation Error

Explanation: i will start from 10.

3. How many times the below loop will get executed? main() { int i,j; i = 10; for (j=i==10 ; j<=10 ; j++) { printf(\n%d,j); } } a. 1 c. 11 Answer : b Explanation: Expression i ==10 return 1 and j get initialized to 1. b. 10 d. Compilation Error

4. How many times the while loop will get executed? main ( )

{ int a = 1 ; while ( a <= 100) ; { printf ( %d, a++ ) ; } } a. 100 c. 0 Answer : d Explanation: Loop will execute infinite no of times because of the ; at the end while loop. b. 1 d. Infinite

5. How many times main() will get called? main( ) { printf ( \nMain called again ) ; main( ) ; } a. 1 c. main cannot be called recursively b. 100 d. Infinite

Answer : d Explanation: There is no condition in the main() to stop the recursive calling of the main() hence it will be called infinite no of times.

6. What will be the output of the following program if the base address of array is 100. main( ) { int gyan[] = { 1, 2, 3, 4, 5 }; int i, *ptr ; ptr = gyan ; for ( i = 0 ; i <4 ; i++ ) { printf ( \n%d, *ptr++ ) ; } } a. 1 2 3 4 c. 100 101 102 103 Answer: a Explanation: ptr contains the base address of the array and printf() is printing the value at the current address contained by ptr and then incrementing the pointer to point to the next array element address. b. 2 3 4 5 d. 101 102 103 104

7. What will be the output of the following program main( ) { int gyan[] = { 10, 20, 30, 40, 50 }; int i, *ptr ; ptr = gyan; for ( i = 0 ; i <4 ; i++ ) { fun(ptr++); printf ( \n%d, *ptr ) ; } } void fun(int *i) { *i = *i + 1; } a. 11 21 31 41 c. 21 31 41 51 Answer: b b. 20 30 40 50 d. 10 20 30 40

Explanation: When we call the function fun() the current address contained by it will get passed and then it get incremented. For ex. If the base address is of the array is 100 then successive elements are stored at 102, 104, 106 and 108. In the first call 100 get passed to fun() and ptr becomes 102. Now the called function fun() manipulates the value stored at 100 and in the main() function values at the address contained by ptr are getting printed.

8. What will be the output main() { char *ptr = Gyantonic.com; char a = printf(%c, ++*ptr++); } a. Compilation Error c. G Answer: b
1.

b. H d. a

What value of c will get printed

main() { int a, b, c; a = 10;

b = 20; c = printf(%d,a) + ++b; printf (%d,c); } a. 23 c. 30 Answer: a Explanation: printf() will return no. of bytes it printed. Expression becomes c = 2 + ++b; then value of b is incremented before addition. 2. How many times the below loop will get executed? main() { int i; for (i=9 ; i ; i=i-2) { printf(\n%d,i); } } a. 5 b. 6 b. 22 d. Compilation Error

c. Compilation Error Answer: d

d. Infinite

Explanation : Above loop will iterate till i have non zero value. Initial value of i is 9 and it get decremented by 2 in every iteration ( 9, 7, 5, 3, 1, -1, -3 .). Value of i will never become 0 and loop will run infinitely.

3. What will be the output main() { int i; i = 10; printf(%d\t,5,6); printf(%d, i , i++); } a. 5 c. 5 11 10 b. 6 d. 6 10 11

Answer: a Explanation: Value in a function get passed from right to left. First i++ get passed and it make i = 11.

4. What the below statement will print if a=10 and b = 20? printf( %d,a==b);

a. 20 c. 10 Answer: b

b. 0 c. 1

Explanation: a==b is a expression and will return 1 (true) or 0 (False) depending on the values of a and b. Here a and b are not equal so 0 is printed.

5. What the below statement will print if a=10? printf( %d %d,a, !a++); a. 11 c. 10 0 0 b. 10 d. 0 10 10

Answer: a Explanation: Values in the function get passed from right to left. First !a++ get processed which pass zero as argument and make a equal to 11.

6. What will be the output main() { int i; i =10; if (i ==20 || 30) { printf(True);

} else { printf(False); } } a. True c. Syntax error Answer : a Explanation: i==20 is a expression which will return TRUE or FALSE depending on the value of i. In this program it will return 0 so the statement become If ( 0 || 30) 30 is a nonzero value which means TRUE (1) in C when ORed with 0 will result TRUE. b. False d. Run time Error

7. What will be the output of the following program? main() { printf(%c,4[Gyantonic]); } a. Compilation Error c. t d n b. G

Answer: c Explanation: Gyantonic is a constant string and character at index 4 will get printed.

8. What will be the output main() { if (1,0) { printf(True); } else { printf(False); } } a. True c. Compilation error Answer : b b. False d. Run time Error

1.

What value of c will get printed

main() { int a, b, c; a = 10; b = 20; c = printf(%d,a) + ++b; printf (%d,c); } a. 23 c. 30 Answer: a Explanation: printf() will return no. of bytes it printed. Expression becomes c = 2 + ++b; then value of b is incremented before addition. 2. How many times the below loop will get executed? main() { int i; for (i=9 ; i ; i=i-2) { b. 22 d. Compilation Error

printf(\n%d,i); } } a. 5 c. Compilation Error Answer: d Explanation : Above loop will iterate till i have non zero value. Initial value of i is 9 and it get decremented by 2 in every iteration ( 9, 7, 5, 3, 1, -1, -3 .). Value of i will never become 0 and loop will run infinitely. b. 6 d. Infinite

3. What will be the output main() { int i; i = 10; printf(%d\t,5,6); printf(%d, i , i++); } a. 5 c. 5 11 10 b. 6 d. 6 10 11

Answer: a

Explanation: Value in a function get passed from right to left. First i++ get passed and it make i = 11.

4. What the below statement will print if a=10 and b = 20? printf( %d,a==b); a. 20 c. 10 Answer: b Explanation: a==b is a expression and will return 1 (true) or 0 (False) depending on the values of a and b. Here a and b are not equal so 0 is printed. b. 0 c. 1

5. What the below statement will print if a=10? printf( %d %d,a, !a++); a. 11 c. 10 0 0 b. 10 d. 0 10 10

Answer: a Explanation: Values in the function get passed from right to left. First !a++ get processed which pass zero as argument and make a equal to 11.

6. What will be the output main() {

int i; i =10; if (i ==20 || 30) { printf(True); } else { printf(False); } } a. True c. Syntax error Answer : a Explanation: i==20 is a expression which will return TRUE or FALSE depending on the value of i. In this program it will return 0 so the statement become If ( 0 || 30) 30 is a nonzero value which means TRUE (1) in C when ORed with 0 will result TRUE. b. False d. Run time Error

7. What will be the output of the following program? main()

{ printf(%c,4[Gyantonic]); } a. Compilation Error c. t Answer: c Explanation: Gyantonic is a constant string and character at index 4 will get printed. d n b. G

8. What will be the output main() { if (1,0) { printf(True); } else { printf(False); } }

a. True c. Compilation error Answer : b

b. False d. Run time Error

Question 1: What is static variable? Ans. Static variable is a variable that has visibility of a local variable and life time of an external variable. It is stored in main memory and has default value zero. Question 2: Where are the auto variables stored? Ans: Auto variables are stored in main memory and their default value is a garbage value. Question 3: Where does global, static, local, register variables and C Program instructions get stored? Ans: Global , static, local : In main memory Register variable: In registers C program : In main memory. Question 4: Why Preincrement operator is faster than Postincrement? Ans: Evaluation of any expression is from left to right. Preincrement is faster because it doesn't need to save the current value for next instruction whereas Postincrement needs to saves current value to be incremented after execution of current instruction. Question 5: In header files whether functions are declared or defined? Ans: Functions in header file are declared. Question 6: Difference between arrays and linked list? Ans: Major differences between arrays and linked lists are: (i) In array consecutive elements are stored in consecutive memory locations whereas in linked list it not so. (ii) In array address of next element is consecutive and whereas in linked list it is specified in the address part of each node. (iii) Linked List makes better use of memory than arrays. (iv) Insertion or deletion of an element in array is difficult than insertion or deletion in linked list. Question 7: What is the use of typedef? Ans: Major uses of typedef are: (i) It increases the portability. (ii) It simplify the complex declaration and improve readability of the program.

Question 8: What are the differences between malloc() and calloc()? Ans: A malloc( ) function allocates a block of memory of the specified size and returns a pointer of specified data type whereas a calloc( ) function allocates a space for an array of elements , initializes them to zero and then returns a pointer to the memory. Question 9: What are the advantages of using pointers in a program? Ans: Major advantages of pointers are: (i) It allows management of structures which are allocated memory dynamically. (ii) It allows passing of arrays and strings to functions more efficiently. (iii) It makes possible to pass address of structure instead of entire structure to the functions. (iv) It makes possible to return more than one value from the function. Question 10: What do the c and v in argc and argv stand for? Ans: c stands for counter and v stands for vector. Question 11: Is function declarations are used only for compilation and not get stored in the .EXE File ? Ans : Yes Question 12: Are the variables argc and argv are local to main? Ans: Yes Question: What is static variable? Ans. Static variable is a variable that has visibility of a local variable and life time of an external variable. It is stored in main memory and has default value zero. Question: Where are the auto variables stored? Ans: Auto variables are stored in main memory and their default value is a garbage value. Question: Where does global, static, local, register variables and C Program instructions get stored? Ans: Global , static, local : In main memory Register variable: In registers C program : In main memory. Question: Why Preincrement operator is faster than Postincrement?

Ans: Evaluation of any expression is from left to right. Preincrement is faster because it doesn't need to save the current value for next instruction whereas Postincrement needs to saves current value to be incremented after execution of current instruction. Question: In header files whether functions are declared or defined? Ans: Functions in header file are declared. Question: Difference between arrays and linked list? Ans: Major differences between arrays and linked lists are: (i) In array consecutive elements are stored in consecutive memory locations whereas in linked list it not so. (ii) In array address of next element is consecutive and whereas in linked list it is specified in the address part of each node.(iii) Linked List makes better use of memory than arrays.(iv) Insertion or deletion of an element in array is difficult than insertion or deletion in linked list. Question: What is the use of typedef? Ans: Major uses of typedef are: (i) It increases the portability. (ii) It simplify the complex declaration and improve readability of the program. Question: What are the differences between malloc() and calloc()? Ans: A malloc( ) function allocates a block of memory of the specified size and returns a pointer of specified data type whereas a calloc( ) function allocates a space for an array of elements , initializes them to zero and then returns a pointer to the memory. Question: What are the advantages of using pointers in a program? Ans: Major advantages of pointers are: (i) It allows management of structures which are allocated memory dynamically. (ii) It allows passing of arrays and strings to functions more efficiently.

(iii) It makes possible to pass address of structure instead of entire structure to the functions. (iv) It makes possible to return more than one value from the function. Question: What do the c and v in argc and argv stand for? Ans: c stands for counter and v stands for vector. Question: Is function declarations are used only for compilation and not get stored in the .EXE File ? Ans : Yes Question: Are the variables argc and argv are local to main? Ans: Yes

Question: What is a structure? Ans: Structure is a collection of heterogeneous (i.e. related data items which can be of different types) held together to a single unit. The data items enclosed within a structure are called its members which may be of data type int, float, char, array etc. Question : What is a pointer? Ans: Pointer is a variable that contains address of another variable in the memory. Pointers are quite useful in creation of linked data structures (such as linked lst, trees graphs), managing object allocated memory dynamically, optimize the program to execute faster and use less memory. Question: What are the different types of Storage classes? Ans: There are four types of storage cases: (i) Extern (ii) Static

(iii) Register (iv) Auto Question: What are the techniques you use for debugging? Ans: Major techniques used for debugging are: (i) Using compilers features (ii) Read The Fine Module (iii) printf( ) debugging (iv) Code grinding (v) Assertion Question: What are macros? What are its advantages and disadvantages? Ans: Macro is a Pre-processor.Major advantage of using the macro is to increase the speed of the execution of the program. Major disadvantage of the macros are: (i) No type checking is performed in macro. This may cause error. (ii) A macro call may cause unexpected results. Question: Does mentioning the array name gives the base address in all the contexts? Ans. No Question: Can a Structure contain a pointer to itself? Ans. Yes, a structure may contain a pointer to itself. Question: Is it mandatory to make Function Declarations global? Ans : No

Question: What are library Functions? Ans: Library Functions are predefined functions and stored in .lib files. Question: What is difference between Structure and Unions? Ans: Major differences between structure and union are: (i) In structure every member has its own memory whereas in union its members share the same member space. (ii) In structure, it is possible to initialize all the members at the same time which is not possible in case of union. (iii) A structure requires more space than union(for the same type of members). (iv) In union different interpretations of the same memory space are possible which is not so in case of structures. Question: What are the advantages of using Unions? Ans: Major advantages of using unions are: (i) Efficient use of memory as it it does not demand memory space for its all members rather it require memory space for its largest member only. (ii) Same memory space can be interpreted differently for different members of the union.

1.

What will be the output

main() { int i , j ,*ptr, *ptr1; i =10;

j = 10; ptr = &i; ptr1 = &j;

if (ptr == ptr1) { printf(True); } else { printf(False); } } a. True c. Syntax error Answer : b Explanation: In this program we are comparing the addresses contained by ptr & ptr1 not the value at those addresses and pointers ptr and ptr1 have the addresses of different variables so above condition is false. b. False d. Run time Error

2. How many times the below loop will get executed? main()

{ int i; for (i=20, i =10 ; i<=20 ; i++) { printf(\n%d,i); } } a. 1 c. 11 Answer : c Explanation: i will start from 10. b. Run time Error d. Compilation Error

3. How many times the below loop will get executed? main() { int i,j; i = 10; for (j=i==10 ; j<=10 ; j++) { printf(\n%d,j); }

} a. 1 c. 11 Answer : b Explanation: Expression i ==10 return 1 and j get initialized to 1. b. 10 d. Compilation Error

4. How many times the while loop will get executed? main ( ) { int a = 1 ; while ( a <= 100) ; { printf ( %d, a++ ) ; } } a. 100 c. 0 Answer : d Explanation: Loop will execute infinite no of times because of the ; at the end while loop. b. 1 d. Infinite

5. How many times main() will get called? main( ) { printf ( \nMain called again ) ; main( ) ; } a. 1 c. main cannot be called recursively Answer : d Explanation: There is no condition in the main() to stop the recursive calling of the main() hence it will be called infinite no of times. b. 100 d. Infinite

6. What will be the output of the following program if the base address of array is 100. main( ) { int gyan[] = { 1, 2, 3, 4, 5 }; int i, *ptr ; ptr = gyan ; for ( i = 0 ; i <4 ; i++ ) { printf ( \n%d, *ptr++ ) ;

} } a. 1 2 3 4 c. 100 101 102 103 Answer: a Explanation: ptr contains the base address of the array and printf() is printing the value at the current address contained by ptr and then incrementing the pointer to point to the next array element address. b. 2 3 4 5 d. 101 102 103 104

7. What will be the output of the following program main( ) { int gyan[] = { 10, 20, 30, 40, 50 }; int i, *ptr ; ptr = gyan; for ( i = 0 ; i <4 ; i++ ) { fun(ptr++); printf ( \n%d, *ptr ) ; } } void fun(int *i)

{ *i = *i + 1; } a. 11 21 31 41 c. 21 31 41 51 Answer: b Explanation: When we call the function fun() the current address contained by it will get passed and then it get incremented. For ex. If the base address is of the array is 100 then successive elements are stored at 102, 104, 106 and 108. In the first call 100 get passed to fun() and ptr becomes 102. Now the called function fun() manipulates the value stored at 100 and in the main() function values at the address contained by ptr are getting printed. b. 20 30 40 50 d. 10 20 30 40

8. What will be the output main() { char *ptr = Gyantonic.com; char a = printf(%c, ++*ptr++); } a. Compilation Error c. G b. H d. a

Answer: b

Вам также может понравиться