57
1. Point out the correct statements are correct about the program below? #include<stdio.h> int main() { char ch; while(x=0;x<=255;x++) printf("ASCII value of %d character %c\n", x, x); return 0; } A. The code generates an infinite loop B. The code prints all ASCII values and its characters C. Error: x undeclared identifier D. Error: while statement missing Answer: Option D Explanation: There are 2 errors in this program. 1. "Undefined symbol x" error. Here x is not defined in the program. 2. Here while() statement syntax error. 2. What will be the output of the program? #include<stdio.h> int main() { int i=-3, j=2, k=0, m; m = ++i && ++j || ++k; printf("%d, %d, %d, %d\n", i, j, k, m); return 0; } A. 1, 2, 0, 1 B. -3, 2, 0, 1 C. -2, 3, 0, 1 D. 2, 3, 1, 1 Answer: Option C Explanation: Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.

Aptitude Questions and Answer

Embed Size (px)

Citation preview

Page 1: Aptitude Questions and Answer

1. Point out the correct statements are correct about the program below? #include<stdio.h>int main(){ char ch; while(x=0;x<=255;x++) printf("ASCII value of %d character %c\n", x, x); return 0;}

A. The code generates an infinite loop

B. The code prints all ASCII values and its characters

C. Error: x undeclared identifier

D. Error: while statement missing

Answer: Option DExplanation:There are 2 errors in this program.1. "Undefined symbol x" error. Here x is not defined in the program.2. Here while() statement syntax error.

2. What will be the output of the program? #include<stdio.h>int main(){ int i=-3, j=2, k=0, m; m = ++i && ++j || ++k; printf("%d, %d, %d, %d\n", i, j, k, m); return 0;}A. 1, 2, 0, 1 B. -3, 2, 0, 1 C. -2, 3, 0, 1 D. 2, 3, 1, 1

Answer: Option CExplanation:Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.

Step 2: m = ++i && ++j || ++k;becomes m = (-2 && 3) || ++k;becomes m = TRUE || ++k;. (++k) is not executed because (-2 && 3) alone return TRUE.Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.

Page 2: Aptitude Questions and Answer

Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of i,j are increemented by '1'(one). Hence the output is "-2, 3, 0, 1".

3. What will be the output of the program? #include<stdio.h>int fun(int, int);typedef int (*pf) (int, int);int proc(pf, int, int);

int main(){ printf("%d\n", proc(fun, 6, 6)); return 0;}int fun(int a, int b){ return (a==b);}int proc(pf p, int a, int b){ return ((*p)(a, b));}A. 6 B. 1 C. 0 D. -1

Answer: Option B

4. What will be the output of the program? #include<stdio.h>#define FUN(arg) do\ {\ if(arg)\ printf("IndiaBIX...", "\n");\ }while(--i)

int main(){ int i=2; FUN(i<3); return 0;}

A. IndiaBIX...IndiaBIX...IndiaBIX

B. IndiaBIX... IndiaBIX...

C. Error: cannot use control instructions in macro

Page 3: Aptitude Questions and Answer

D. No output

Answer: Option BExplanation:

The macro FUN(arg) prints the statement "IndiaBIX..." untill the while condition is satisfied.

Step 1: int i=2; The variable i is declared as an integer type and initialized to 2.

Step 2: FUN(i<3); becomes,

do{ if(2 < 3) printf("IndiaBIX...", "\n");}while(--2)

After the 2 while loops the value of i becomes '0'(zero). Hence the while loop breaks.Hence the output of the program is "IndiaBIX... IndiaBIX..."

5. What will be the output of the program? #include<stdio.h>#define MAX(a, b) (a > b ? a : b)

int main(){ int x; x = MAX(3+2, 2+7); printf("%d\n", x); return 0;}A. 8 B. 9C. 6 D. 5

Answer: Option BExplanation:

The macro MAX(a, b) (a > b ? a : b) returns the biggest value of the given two numbers. Step 1 : int x; The variable x is declared as an integer type.

Step 2 : x = MAX(3+2, 2+7); becomes,

=> x = (3+2 > 2+7 ? 3+2 : 2+7)

=> x = (5 > 9 ? 5 : 9)

=> x = 9

Page 4: Aptitude Questions and Answer

Step 3 : printf("%d\n", x); It prints the value of variable x. Hence the output of the program is 9.

6. What will be the output of the program? #include<stdio.h>#define MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c)

int main(){ int x; x = MAX(3+2, 2+7, 3+7); printf("%d\n", x); return 0;}A. 5 B. 9 C. 10 D. 3+7

Answer: Option CExplanation:The macro MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c) returns the biggest of given three numbers.

Step 1: int x; The variable x is declared as an integer type. Step 2: x = MAX(3+2, 2+7, 3+7); becomes,

=> x = (3+2 >2+7 ? 3+2 > 3+7 ? 3+2 : 3+7: 2+7 > 3+7 ? 2+7 : 3+7)

=> x = (5 >9 ? (5 > 10 ? 5 : 10): (9 > 10 ? 9 : 10) )

=> x = (5 >9 ? (10): (10) )

=> x = 10

Step 3: printf("%d\n", x); It prints the value of 'x'. Hence the output of the program is "10".

7. What will be the output of the program ? #include<stdio.h>

int main(){ int i, a[] = {2, 4, 6, 8, 10}; change(a, 5); for(i=0; i<=4; i++) printf("%d, ", a[i]); return 0;}change(int *b, int n){

Page 5: Aptitude Questions and Answer

int i; for(i=0; i<n; i++) *(b+1) = *(b+i)+5;}A. 7, 9, 11, 13, 15 B. 2, 15, 6, 8, 10 C. 2 4 6 8 10 D. 3, 1, -1, -3, -5

Answer: Option B

8. In the following program add a statement in the function fact(). such that the factorial gets stored in j. #include<stdio.h>void fact(int*);

int main(){ int i=5, j; fact(&i); printf("%d\n", i); return 0;}void fact(int *j){ static int s=1; if(*j!=0) { s = s**j; *j = *j-1; fact(j); /* Add a statement here */ }}A. j=s; B. *j=s;C. *j=&s; D. &j=s;

Answer: Option B

9. Is the NULL pointer same as an uninitialised pointer?A. Yes B. No

Answer: Option B

10. What will be the output of the program if the array begins at address 65486? #include<stdio.h>

int main(){ int arr[] = {12, 14, 15, 23, 45}; printf("%u, %u\n", arr, &arr);

Page 6: Aptitude Questions and Answer

return 0;}A. 65486, 65488 B. 65486, 65486C. 65486, 65490 D. 65486, 65487

Answer: Option BExplanation:

Step 1: int arr[] = {12, 14, 15, 23, 45}; The variable arr is declared as an integer array and initialized. Step 2: printf("%u, %u\n", arr, &arr); Here,

The base address of the array is 65486.

=> arr, &arr is pointing to the base address of the array arr.Hence the output of the program is 65486, 65486

11. What will be the output of the program ? #include<stdio.h>#include<string.h>

int main(){ char str1[20] = "Hello", str2[20] = " World"; printf("%s\n", strcpy(str2, strcat(str1, str2))); return 0;}A. Hello B. WorldC. Hello World D. WorldHello

Answer: Option CExplanation:

Step 1: char str1[20] = "Hello", str2[20] = " World"; The variable str1 and str2 is declared as an array of characters and initialized with value "Hello" and " World" respectively. Step 2: printf("%s\n", strcpy(str2, strcat(str1, str2)));

=> strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains "Hello World".

=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2.Hence it prints "Hello World".

12. For the following statements will arr[3] and ptr[3] fetch the same character?char arr[] = "IndiaBIX";char *ptr = "IndiaBIX";A. Yes B. No

Page 7: Aptitude Questions and Answer

Answer: Option AExplanation:

Yes, both the statements prints the same character 'i'. 13. It is not possible to create an array of pointer to structures.A. True B. False

Answer: Option B

14. Can I increase the size of statically allocated array?A. Yes B. No

Answer: Option B

15. What will be the output of the program? #include<stdio.h>#include<stdarg.h>void fun1(int num, ....);void fun2(int num, ....);

int main(){ fun1(1, "Apple", "Boys", "Cats", "Dogs"); fun2(2, 12, 13, 14); return 0;}void fun1(int num, ...){ char *str; va_list ptr; va_start(ptr, num); str = va_arg(ptr, char *); printf("%s", str);}void fun2(int num, ...){ va_list ptr; va_start(ptr, num); num = va_arg(ptr, int); printf("%d", num);}A. Dogs 12 B. Cats 14C. Boys 13 D. Apple 12

Answer: Option D

16. Point out the error in the following program. #include<stdio.h>#include<stdarg.h>

Page 8: Aptitude Questions and Answer

void display(char *s, ...);void show(char *t, ...);

int main(){ display("Hello", 4, 12, 13, 14, 44); return 0;}void display(char *s, ...){ show(s, ...);}void show(char *t, ...){ int a; va_list ptr; va_start(ptr, s); a = va_arg(ptr, int); printf("%f", a);}

A. Error: invalid function display() call

B. Error: invalid function show() call

C. No error

D. Error: Rvalue required for t

Answer: Option B

Explanation:The call to show() is improper. This is not the way to pass variable argument list to a function.

17. In a function that receives variable number of arguments the fixed arguments passed to the function can be at the end of argument list.

A. True B. False

Answer: Option B

18. What will be the output of the program? #include<stdio.h>

int main(){ char huge *near *far *ptr1; char near *far *huge *ptr2; char far *huge *near *ptr3; printf("%d, %d, %d\n", sizeof(ptr1), sizeof(*ptr2), sizeof(**ptr3));

Page 9: Aptitude Questions and Answer

return 0;}

A. 4, 4, 4 B. 2, 4, 4 C. 4, 4, 2 D. 2, 4, 8

Answer: Option A

19. What will be the output of the program? #include<stdio.h>

int main(){ char huge *near *ptr1; char huge *far *ptr2; char huge *huge *ptr3; printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3)); return 0;}

A. 4, 4, 8 B. 2, 4, 4C. 4, 4, 2 D. 2, 4, 8

Answer: Option B

20. ftell() returns the current position of the pointer in a file stream.A. True B. False

Answer: Option A

Explanation:

The ftell() function shall obtain the current value of the file-position indicator for the stream pointed to by stream.

Example:

#include <stdio.h>

int main(void){ FILE *stream; stream = fopen("MYFILE.TXT", "w+"); fprintf(stream, "This is a test"); printf("The file pointer is at byte %ld\n", ftell(stream)); fclose(stream); return 0;}

Page 10: Aptitude Questions and Answer

1. Which of the following are unary operators in C? 1.! 2.sizeof 3.~ 4.&&

A. 1, 2 B. 1, 3C. 2, 4 D. 1, 2, 3

Answer: Option D

Explanation:

An operation with only one operand is called unary operation.Unary operators:! Logical NOT operator.~ bitwise NOT operator.sizeof Size-of operator.

&& Logical AND is a logical operator.

Therefore, 1, 2, 3 are unary operators.

2. Two different operators would always have different Associativity.A. Yes B. No

Answer: Option B

Explanation:

No, Two different operators may have same associativity.

Example:

Page 11: Aptitude Questions and Answer

Arithmetic operators like ++, -- having Right-to-Left associativity.Relational operators like >, >= also have Left-to-Right associativity.

3. Will the expression *p = c be disallowed by the compiler?A. Yes B. No

Answer: Option B

Explanation:Because, here even though the value of p is accessed twice it is used to modify two different objects p and *p

4. What will you do to treat the constant 3.14 as a long double?A. use 3.14LD B. use 3.14LC. use 3.14DL D. use 3.14LF

Answer: Option B

Explanation:

Given 3.14 is a double constant.

To specify 3.14 as long double, we have to add L to the 3.14. (i.e 3.14L)

5. Preprocessor directive #undef can be used only on a macro that has been #define earlier

A. True B. False

Answer: Option A

Explanation:

True, #undef can be used only on a macro that has been #define earlier

Example: #define PI 3.14

We can undefine PI macro by #undef PI

6. What will be the output of the program ? #include<stdio.h>

void fun(void *p);int i;

int main(){ void *vptr; vptr = &i;

Page 12: Aptitude Questions and Answer

fun(vptr); return 0;}void fun(void *p){ int **q; q = (int**)&p; printf("%d\n", **q);}A. Error: cannot convert from void** to int** B. Garbage valueC. 0 D. No output

Answer: Option C

7. Will the program compile? #include<stdio.h>int main(){ int a=10, *j; void *k; j=k=&a; j++; k++; printf("%u %u\n", j, k); return 0;}

A. Yes B. No

Answer: Option B

Explanation:

Error in statement k++. We cannot perform arithmetic on void pointers.

The following error will be displayed while compiling above program in TurboC.

Compiling PROGRAM.C:Error PROGRAM.C 8: Size of the type is unknown or zero.

8. The library function used to reverse a string isA. strstr() B. strrev()C. revstr() D. strreverse()

Answer: Option B

Page 13: Aptitude Questions and Answer

Explanation:

strrev(s) Reverses all characters in s

Example:#include <string.h>#include <stdio.h>

int main(void){ char *str = "IndiaBIX";

printf("Before strrev(): %s\n", str); strrev(str); printf("After strrev(): %s\n", str); return 0;}

Output:

Before strrev(): IndiaBIX

After strrev(): XIBaidnI

9. Which of the following function is more appropriate for reading in a multi-word string?A. printf(); B. scanf();C. gets(); D. puts();

Answer: Option C

Explanation:

gets(); collects a string of characters terminated by a new line from the standard input stream stdin #include <stdio.h>

int main(void){ char string[80];

printf("Enter a string:"); gets(string); printf("The string input was: %s\n", string); return 0;}

Output:

Enter a string: IndiaBIX

Page 14: Aptitude Questions and Answer

The string input was: IndiaBIX

10. Out of fgets() and gets() which function is safe to use?A. gets() B. fgets()

Answer: Option B

Explanation:Because, In fgets() we can specify the size of the buffer into which the string supplied will be stored.

11. What will be the output of the program ? #include<stdio.h>

int main(){ FILE *ptr; char i; ptr = fopen("myfile.c", "r"); while((i=fgetc(ptr))!=NULL) printf("%c", i); return 0;}

A. Print the contents of file "myfile.c"

B. Print the contents of file "myfile.c" upto NULL character

C. Infinite loop

D. Error in program

Answer: Option C

Explanation:The program will generate infinite loop. When an EOF is encountered fgetc() returns EOF. Instead of checking the condition for EOF we have checked it for NULL. so the program will generate infinite loop.

12. Which bitwise operator is suitable for checking whether a particular bit is on or off?A. && operator B. & operatorC. || operator D. ! operator

Answer: Option B

13. What will be the output of the program? #include<stdio.h>

int main()

Page 15: Aptitude Questions and Answer

{ unsigned int res; res = (64 >>(2+1-2)) & (~(1<<2)); printf("%d\n", res); return 0;}

A. 32 B. 64C. 0 D. 128

Answer: Option A

14. Is there any difference in the #define and typedef in the following code? typedef char * string_t;#define string_d char *;string_t s1, s2;string_d s3, s4;

A. YesB. No

Answer: Option A

Explanation:In these declarations, s1, s2 and s3 are all treated as char*, but s4 is treated as a char, which is probably not the intention.

15. Are the properties of i, j and x, y in the following program same? typedef unsigned long int uli;uli i, j;unsigned long int x, y;

A. Yes B. No

Answer: Option A

16. Point out the error in the program. #include<stdio.h>#define MAX 128

int main(){ const int max=128; char array[max]; char string[MAX]; array[0] = string[0] = 'A'; printf("%c %c\n", array[0], string[0]); return 0;}

Page 16: Aptitude Questions and Answer

A. Error: unknown max in declaration

B. Error: invalid array string

C. None of above

D. No error. It prints A A

Answer: Option D

Explanation:

Step 1: A macro named MAX is defined with value 128

Step 2: const int max=128; The constant variable max is declared as an integer data type and it is initialized with value 128.

Step 3: char array[max]; This statement reports an error "constant expression required". Because, we cannot use variable to define the size of array.

To avoid this error, we have to declare the size of an array as static. Eg. char array[10]; or use macro char array[MAX];

17. Point out the correct statement which correctly free the memory pointed to by 's' and 'p' in the following program? #include<stdio.h>#include<stdlib.h>

int main(){ struct ex { int i; float j; char *s }; struct ex *p; p = (struct ex *)malloc(sizeof(struct ex)); p->s = (char*)malloc(20); return 0;}A. free(p); , free(p->s); B. free(p->s); , free(p);C. free(p->s); D. free(p);

Answer: Option B

18. What will be the output of the program? #include<stdio.h>#include<stdarg.h>void fun(char *msg, ....);

Page 17: Aptitude Questions and Answer

int main(){ fun("IndiaBIX", 1, 4, 7, 11, 0); return 0;}void fun(char *msg, ....){ int tot=0; va_list ptr; int num; va_start(ptr, msg); num = va_arg(ptr, int) num = va_arg(ptr, int) printf("%d", num);}

A. IndiaBIX 1 7 11 0 B. 1C. 4 D. 7

Answer: Option C

19. What will be the output of the program? #include<stdio.h>

int main(){ int i; char c; for(i=1; i<=5; i++) { scanf("%c", &c); /* given input is 'a' */ printf("%c", c); ungetc(c, stdin); } return 0;}A. aaaa B. aaaaaC. Garbage value. D. Error in ungetc statement.

Answer: Option B

Explanation:

for(i=1; i<=5; i++) Here the for loop runs 5 times.

Loop 1: scanf("%c", &c); Here we give 'a' as input. printf("%c", c); prints the character 'a' which is given in the previous "scanf()" statement.

Page 18: Aptitude Questions and Answer

ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

Loop 2: Here the scanf("%c", &c); get the input from "stdin" because of "ungetc" function. printf("%c", c); Now variable c = 'a'. So it prints the character 'a'. ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

This above process will be repeated in Loop 3, Loop 4, Loop 5.

20. scanf() or atoi() function can be used to convert a string like "436" in to integer.

A. Yes B. No

Answer: Option A

Explanation:

scanf is a function that reads data with specified format from a given string stream source. scanf("%d",&number);

atoi() convert string to integer. var number;number = atoi("string");

1. Point out the error, if any in the program. #include<stdio.h>int main(){ int i = 1; switch(i) { case 1: printf("Case1"); break; case 1*2+4: printf("Case2"); break; }return 0;}

A. Error: in case 1*2+4 statement

B. Error: No default specified

Page 19: Aptitude Questions and Answer

C. Error: in switch statement

D. No Error

Answer: Option D

Explanation:

Constant expression are accepted in switch

It prints "Case1"

2. Are the following two statement same? 1. a <= 20 ? b = 30: c = 30;2. (a <=20) ? b : c = 30;

A. Yes B. No

Answer: Option B

Explanation:

No, the expressions 1 and 2 are not same.

1. a <= 20 ? b = 30: c = 30; This statement can be rewritten as,

if(a <= 20){ b = 30;}else{ c = 30;}

2. (a <=20) ? b : c = 30; This statement can be rewritten as,

if(a <= 20){ //Nothing here}else{ c = 30;}

3. Macro calls and function calls work exactly similarly.A. True B. False

Page 20: Aptitude Questions and Answer

Answer: Option B

Explanation:

False, A macro just replaces each occurrence with the code assigned to it. e.g. SQUARE(3) with ((3)*(3)) in the program.

A function is compiled once and can be called from anywhere that has visibility to the funciton.

4. What will be the output of the program ? #include<stdio.h>

int main(){ char str[20] = "Hello"; char *const p=str; *p='M'; printf("%s\n", str); return 0;}

A. Mello B. Hello

C. HMelloD. MHello

Answer: Option A

5. What will be the output of the program ? #include<stdio.h>

int main(){ printf("%c\n", 7["IndiaBIX"]); return 0;}

A. Error: in printfB. Nothing will print

C. print "X" of IndiaBIX D. print "7"

Answer: Option C

Page 21: Aptitude Questions and Answer

6. In the following program add a statement in the function fun() such that address of a gets stored in j? #include<stdio.h>int main(){ int *j; void fun(int**); fun(&j); return 0;}void fun(int **k){ int a=10; /* Add a statement here */}

A. **k=a; B. k=&a;

C. *k=&aD. &k=*a

Answer: Option C

7. What will be the output of the program ? #include<stdio.h>

int main(){ static int arr[] = {0, 1, 2, 3, 4}; int *p[] = {arr, arr+1, arr+2, arr+3, arr+4}; int **ptr=p; ptr++; printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr); *ptr++; printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr); *++ptr; printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr); ++*ptr; printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr); return 0;}A. 0, 0, 0 1, 1, 1 2, 2, 2 3, 3, 3B. 1, 1, 2 2, 2, 3 3, 3, 4

Page 22: Aptitude Questions and Answer

4, 4, 1

C. 1, 1, 1 2, 2, 2 3, 3, 3 3, 4, 4D. 0, 1, 2 1, 2, 3 2, 3, 4 3, 4, 5

Answer: Option C

8. What will be the output of the program ? #include<stdio.h>

int main(){ union var { int a, b; }; union var v; v.a=10; v.b=20; printf("%d\n", v.a); return 0;}

A. 10B. 20

C. 30D. 0

Answer: Option B

9. Point out the error in the program? #include<stdio.h>

int main(){ struct a { category:5; scheme:4; }; printf("size=%d", sizeof(struct a)); return 0;

Page 23: Aptitude Questions and Answer

}A. Error: invalid structure member in printf

B. Error: bit field type must be signed int or unsigned int

C. No error

D. None of above

Answer: Option B

10. Point out the error in the program? #include<stdio.h>

int main(){ struct bits { float f:2; }bit;

printf("%d\n", sizeof(bit)); return 0;}

A. 4

B. 2

C. Error: cannot set bit field for float

D. Error: Invalid member access in structure

Answer: Option C

11. A pointer union CANNOT be createdA. Yes

B. No

Answer: Option B

12. Will the following declaration work? typedef struct s{ int a;

Page 24: Aptitude Questions and Answer

float b;}s;

A. Yes B. No

Answer: Option A

13. Can a structure can point to itself?A. Yes

B. No

Answer: Option A

Explanation:A structure pointing to itself is called self-referential structures.

14. How will you use the following program to copy the contents of one file to another file? #include<stdio.h>

int main(){ char ch, str[10]; while((ch=getc(stdin))!= EOF) putc(ch, stdout); return 0;}

A. mycopy > sourcefile > targetfile

B. mycopy < sourcefile > targetfile

C. mycopy > sourcefile < targetfile

D. mycopy < sourcefile < targetfile

Answer: Option B

Explanation:

Compile this file and it makes the mycopy.exe file.

Open the command prompt and type

mycopy <source.txt> target.txt

Page 25: Aptitude Questions and Answer

The contents of the source.txt is copied in to target.txt

15. We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind()

A. TrueB. False

Answer: Option A

Explanation:

True, we should not be able to read a file after writing in that file without calling the below functions.

int fflush ( FILE * stream ); If the given stream was open for writing and the last i/o operation was an output operation, any unwritten data in the output buffer is written to the file.

int fseek ( FILE * stream, long int offset, int origin ); Its purpose is to change the file position indicator for the specified stream.

void rewind ( FILE * stream ); Sets the position indicator associated with stream to the beginning of the file.

16. In which numbering system can the binary number 1011011111000101 be easily converted to?

A. Decimal systemB. Hexadecimal system

C. Octal systemD. No need to convert

Answer: Option B

Explanation:Hexadecimal system is better, because each 4-digit binary represents one Hexadecimal digit.

17. What will be the output of the program? #define P printf("%d\n", -1^~0);#define M(P) int main()\ {\ P\ return 0;\ }

Page 26: Aptitude Questions and Answer

M(P)A. 1

B. 0

C. -1 D. 2

Answer: Option B

18. What will be the output of the program? #include<stdio.h>

int main(){ enum color{red, green, blue}; typedef enum color mycolor; mycolor m = red; printf("%d", m); return 0;}

A. 1B. 0

C. 2 D. red

Answer: Option B

19. While defining a variable argument list function we drop the ellipsis(...)?

A. YesB. No

Answer: Option A

20. What will be the output of the program? #include<stdio.h>

int main(){ char huge *near *far *ptr1; char near *far *huge *ptr2; char far *huge *near *ptr3; printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));

Page 27: Aptitude Questions and Answer

return 0;}

A. 4, 4, 8 B. 2, 4, 4

C. 4, 4, 2D. 2, 4, 8

Answer: Option C

1. What will be the output of the program? #include<stdio.h>int main(){

Page 28: Aptitude Questions and Answer

int a = 500, b = 100, c; if(!a >= 400) b = 300; c = 200; printf("b = %d c = %d\n", b, c); return 0;}

A. b = 300 c = 200B. b = 100 c = garbage

C. b = 300 c = garbage D. b = 100 c = 200

Answer: Option D

Explanation:

Initially variables a = 500, b = 100 and c is not assigned.

Step 1: if(!a >= 400)Step 2: if(!500 >= 400)Step 3: if(0 >= 400)Step 4: if(FALSE) Hence the if condition is failed.Step 5: So, variable c is assigned to a value '200'.Step 6: printf("b = %d c = %d\n", b, c); It prints value of b and c.Hence the output is "b = 100 c = 200"

2. What will be the output of the program? #include<stdio.h>int check (int, int);

int main(){ int c; c = check(10, 20); printf("c=%d\n", c); return 0;}int check(int i, int j){ int *p, *q; p=&i; q=&j; i>=45 ? return(*p): return(*q);}

A. Print 10B. Print 20

Page 29: Aptitude Questions and Answer

C. Print 1D. Compile error

Answer: Option D

Explanation:

There is an error in this line i>=45 ? return(*p): return(*q);. We cannot use return keyword in the terenary operators.

3. Point out the error in the program #include<stdio.h>#define SI(p, n, r) float si; si=p*n*r/100;int main(){ float p=2500, r=3.5; int n=3; SI(p, n, r); SI(1500, 2, 2.5); return 0;}

A. 26250.00 7500.00B. Nothing will print

C. Error: Multiple declaration of siD. Garbage values

Answer: Option C

Explanation:

The macro #define SI(p, n, r) float si; si=p*n*r/100; contains the error. To remove this error, we have to modify this macro to

#define SI(p,n,r) p*n*r/100

4. A preprocessor directive is a message from programmer to the preprocessor.

A. True B. False

Answer: Option A

Explanation:

Page 30: Aptitude Questions and Answer

True, the programmer tells the compiler to include the preprocessor when compiling.

5. A header file contains macros, structure declaration and function prototypes.

A. TrueB. False

Answer: Option A

Explanation:

True, the header file contains classes, function prototypes, structure declaration, macros.

6. What will be the output of the program ? #include<stdio.h>

int main(){ void fun(int, int[]); int arr[] = {1, 2, 3, 4}; int i; fun(4, arr); for(i=0; i<4; i++) printf("%d,", arr[i]); return 0;}void fun(int n, int arr[]){ int *p=0; int i=0; while(i++ < n) p = &arr[i]; *p=0;}

A. 2, 3, 4, 5B. 1, 2, 3, 4

C. 0, 1, 2, 3 D. 3, 2, 1 0

Answer: Option B

Explanation:

Page 31: Aptitude Questions and Answer

Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts one integer value and one array as an arguments and does not return anything.

Step 2: int arr[] = {1, 2, 3, 4}; The variable a is declared as an integer array and it is initialized to

a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4

Step 3: int i; The variable i is declared as an integer type.

Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function.

Step 5: for(i=0; i<4; i++) { printf("%d,", arr[i]); } The for loop runs untill the variable i is less than '4' and it prints the each value of array a.

Hence the output of the program is 1,2,3,4

7. What will be the output of the program ? #include<stdio.h>

int main(){ char t; char *p1 = "India", *p2; p2=p1; p1 = "BIX"; printf("%s %s\n", p1, p2); return 0;}

A. India BIXB. BIX India

C. India IndiaD. BIX BIX

Answer: Option B

Explanation:

Step 1: char *p1 = "India", *p2; The variable p1 and p2 is declared as an pointer to a character value and p1 is assigned with a value "India".

Step 2: p2=p1; The value of p1 is assigned to variable p2. So p2 contains "India".

Step 3: p1 = "BIX"; The p1 is assigned with a string "BIX"

Page 32: Aptitude Questions and Answer

Step 4: printf("%s %s\n", p1, p2); It prints the value of p1 and p2.

Hence the output of the program is "BIX India".

8. What will be the output of the program ? #include<stdio.h>

int main(){ struct emp { char *n; int age; }; struct emp e1 = {"Dravid", 23}; struct emp e2 = e1; strupr(e2.n); printf("%s\n", e1.n); return 0;}

A. Error: Invalid structure assignment

B. DRAVID

C. Dravid

D. No output

Answer: Option B

9. Point out the error in the program? #include<stdio.h>

int main(){ struct emp { char name[25]; int age; float bs; }; struct emp e; e.name = "Suresh"; e.age = 25; printf("%s %d\n", e.name, e.age); return 0;}

Page 33: Aptitude Questions and Answer

A. Error: Lvalue required

B. Error: invalid structure assignment

C. Error: Rvalue required

D. No error

Answer: Option A

10. What do the 'c' and 'v' in argv stands for?A. 'c' means argument control 'v' means argument vector

B. 'c' means argument count 'v' means argument vertex

C. 'c' means argument count 'v' means argument vector

D. 'c' means argument configuration 'v' means argument visibility

Answer: Option C

11. Which of the following statements are correct about the program? #include<stdio.h>

int main(){ unsigned int num; int i; scanf("%u", &num); for(i=0; i&lt16; i++) { printf("%d", (num<<i & 1<<15)?1:0); } return 0;}

A. It prints all even bits from num

B. It prints all odd bits from num

C. It prints binary equivalent num

D. Error

Answer: Option C

Page 34: Aptitude Questions and Answer

12. What will be the output of the program? #include<stdio.h>

typedef struct error {int warning, err, exception;} ERROR;int main(){ ERROR e; e.err=1; printf("%d\n", e.err); return 0;}

A. 0 B. 1

C. 2D. Error

Answer: Option B

13. What will be the output of the program? #include<stdio.h>#include<stdlib.h>

union employee{ char name[15]; int age; float salary;};const union employee e1;

int main(){ strcpy(e1.name, "K"); printf("%s %d %f", e1.name, e1.age, e1.salary); return 0;}

A. Error: RValue required

B. Error: cannot convert from 'const int *' to 'int *const'

C. Error: LValue required in strcpy

D. No error

Page 35: Aptitude Questions and Answer

Answer: Option D

Explanation:

The output will be:

K 75 0.000000

14. Point out the error in the program. #include<stdio.h>#include<stdlib.h>int fun(const union employee *e);

union employee{ char name[15]; int age; float salary;};const union employee e1;

int main(){ strcpy(e1.name, "A"); fun(&e1); printf("%s %d %f", e1.name, e1.age, e1.salary); return 0;}int fun(const union employee *e){ strcpy((*e).name, "B"); return 0;}

A. Error: RValue required

B. Error: cannot convert parameter 1 from 'const char[15]' to 'char *'

C. Error: LValue required in strcpy

D. No error

Answer: Option B

15. How will you free the memory allocated by the following program? #include<stdio.h>#include<stdlib.h>#define MAXROW 3

Page 36: Aptitude Questions and Answer

#define MAXCOL 4

int main(){ int **p, i, j; p = (int **) malloc(MAXROW * sizeof(int*)); return 0;}

A. memfree(int p); B. dealloc(p);

C. malloc(p, 0);D. free(p);

Answer: Option D

16. malloc() returns a float pointer if memory is allocated for storing float's and a double pointer if memory is allocated for storing double's.

A. True B. False

Answer: Option B

17. What will be the output of the program? #include<stdio.h>#include<stdarg.h>void display(int num, ....);

int main(){ display(4, 'A', 'B', 'C', 'D'); return 0;}void display(int num, ...){ char c, c1; int j; va_list ptr, ptr1; va_start(ptr, num); va_start(ptr1, num); for(j=1; j<=num; j++) { c = va_arg(ptr, int); printf("%c", c); c1 = va_arg(ptr1, int); printf("%d\n", c1); }

Page 37: Aptitude Questions and Answer

}A. A, A

B, BC, CD, D

B. A, aB, bC, cD, d

C. A, 65B, 66C, 67D, 68

D. A, 0B, 0C, 0C, 0

Answer: Option C

18. The macro va_arg is used to extract an argument from the fixed micro argument list and advance the pointer to the next argument.

A. Yes B. No

Answer: Option B

19. Declare the following statement?"A pointer to an array of three chars".

A. char *ptr[3]();B. char (*ptr)*[3];

C. char (*ptr[3])();D. char (*ptr)[3];

Answer: Option D

20. Are the following declarations same? char far *far *scr;char far far** scr;

A. YesB. No

Page 38: Aptitude Questions and Answer

Answer: Option B

1. Which of the following statements are correct about the program? #include<stdio.h>int main(){ int x = 30, y = 40; if(x == y) printf("x is equal to y\n");

else if(x > y) printf("x is greater than y\n");

else if(x < y) printf("x is less than y\n") return 0;}

A. Error: Statement missingB. Error: Expression syntax

C. Error: Lvalue required D. Error: Rvalue required

Answer: Option A

Explanation:

This program will result in error "Statement missing ;"

printf("x is less than y\n") here ; is added to the end of this statement.

2. The modulus operator cannot be used with a long double.A. True

B. False

Answer: Option A

Explanation:

Page 39: Aptitude Questions and Answer

fmod(x,y) - Calculates x modulo y, the remainder of x/y. This function is the same as the modulus operator. But fmod() performs floating point or long double divisions.

3. Which of the following correctly shows the hierarchy of arithmetic operations in C?

A. / + * -B. * - / +

C. + - / * D. / * + -

Answer: Option D

Explanation:

Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction).

How Do I Remember ? BODMAS !

B - Brackets firstO - Orders (ie Powers and Square Roots, etc.)DM - Division and Multiplication (left-to-right)AS - Addition and Subtraction (left-to-right)

4. What will be the output of the program? #include<stdio.h>

int main(){ void fun(char*); char a[100]; a[0] = 'A'; a[1] = 'B'; a[2] = 'C'; a[3] = 'D'; fun(&a[0]); return 0;}void fun(char *a){ a++; printf("%c", *a); a++; printf("%c", *a);}

A. ABB. BC

Page 40: Aptitude Questions and Answer

C. CD D. No output

Answer: Option B

5. If the size of integer is 4bytes. What will be the output of the program? #include<stdio.h>

int main(){ int arr[] = {12, 13, 14, 15, 16}; printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0])); return 0;}

A. 10, 2, 4B. 20, 4, 4

C. 16, 2, 2D. 20, 2, 2

Answer: Option B

6. What will be the output of the program ? #include<stdio.h>

int main(){ printf(5+"Good Morning\n"); return 0;}

A. Good Morning B. Good

C. MD. Morning

Answer: Option D

Explanation:

printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string.

Page 41: Aptitude Questions and Answer

Hence the output is "Morning"

7. What will be the output of the program If characters 'a', 'b' and 'c' enter are supplied as input? #include<stdio.h>

int main(){ void fun(); fun(); printf("\n"); return 0;}void fun(){ char c; if((c = getchar())!= '\n') fun(); printf("%c", c);}

A. abc abcB. bca

C. Infinite loopD. cba

Answer: Option D

Explanation:

Step 1: void fun(); This is the prototype for the function fun().

Step 2: fun(); The function fun() is called here.

The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order.

The given input characters are "abc"

Output: cba

8. Which of the following statements are correct about the program below? #include<stdio.h>

int main(){ char str[20], *s;

Page 42: Aptitude Questions and Answer

printf("Enter a string\n"); scanf("%s", str); s=str; while(*s != '\0') { if(*s >= 97 && *s <= 122) *s = *s-32; s++; } printf("%s",str); return 0;}

A. The code converts a string in to an integer

B. The code converts lower case character to upper case

C. The code converts upper case character to lower case

D. Error in code

Answer: Option B

Explanation:

This program converts the given string to upper case string.

Output:

Enter a string: indiabix

INDIABIX

9. What will be the output of the program ? #include<stdio.h>

struct course { int courseno; char coursename[25]; };int main(){ struct course c[] = { {102, "Java"}, {103, "PHP"}, {104, "DotNet"} };

printf("%d", c[1].courseno); printf("%s\n", (*(c+2)).coursename);

Page 43: Aptitude Questions and Answer

return 0;}

A. 103 DotnetB. 102 Java

C. 103 PHPD. 104 DotNet

Answer: Option A

10. If the file 'source.txt' contains a line "Be my friend" which of the following will be the output of below program? #include<stdio.h>

int main(){ FILE *fs, *ft; char c[10]; fs = fopen("source.txt", "r"); c = getc(fs); fseek(fs, 0, SEEK_END); fseek(fs, -3L, SEEK_CUR); fgets(c, 5, fs); puts(c); return 0;}

A. friendB. frien

C. endD. Error in fseek();

Answer: Option C

Explanation:

The file source.txt contains "Be my friend".

fseek(fs, 0, SEEK_END); moves the file pointer to the end of the file.

fseek(fs, -3L, SEEK_CUR); moves the file pointer backward by 3 characters.

fgets(c, 5, fs); read the file from the current position of the file pointer.

Hence, it contains the last 3 characters of "Be my friend".

Page 44: Aptitude Questions and Answer

Therefore, it prints "end".

11. Point out the error in the program? #include<stdio.h>

int main(){ FILE *fp; fp=fopen("trial", "r"); fseek(fp, 20, SEEK_SET); fclose(fp); return 0;}

A. Error: unrecognised Keyword SEEK_SET

B. Error: fseek() long offset value

C. No error

D. None of above

Answer: Option B

Explanation:Instead of 20 use 20L since fseek() need a long offset value.

12. What will be the output of the program (myprog.c) given below if it is executed from the command line?cmd> myprog one two three /* myprog.c */#include<stdio.h>#include<stdlib.h>

int main(int argc, char **argv){ printf("%s\n", *++argv); return 0;}

A. myprogB. one

C. twoD. three

Answer: Option B

Page 45: Aptitude Questions and Answer

13. What will be the output of the program (sample.c) given below if it is executed from the command line?cmd> sample 1 2 3 /* sample.c */#include<stdio.h>

int main(int argc, char *argv[]){ int j; j = argv[1] + argv[2] + argv[3]; printf("%d", j); return 0;}

A. 6B. sample 6

C. Error D. Garbage value

Answer: Option C

14. What will be the output of the program (sample.c) given below if it is executed from the command line?cmd> sample friday tuesday sunday /* sample.c */#include<stdio.h>

int main(int sizeofargv, char *argv[]){ while(sizeofargv) printf("%s", argv[--sizeofargv]); return 0;}

A. sample friday tuesday sunday

B. sample friday tuesday

C. sunday tuesday friday sample

D. sunday tuesday friday

Answer: Option C

15. Which of the following statements are correct about the program? #include<stdio.h>char *fun(unsigned int num, int base);

Page 46: Aptitude Questions and Answer

int main(){ char *s; s=fun(128, 2); s=fun(128, 16); printf("%s\n",s); return 0;}char *fun(unsigned int num, int base){ static char buff[33]; char *ptr = &buff[sizeof(buff)-1]; *ptr = '\0'; do { *--ptr = "0123456789abcdef"[num %base]; num /=base; }while(num!=0); return ptr;}

A. It converts a number to a given base.

B. It converts a number to its equivalent binary.

C. It converts a number to its equivalent hexadecimal.

D. It converts a number to its equivalent octal.

Answer: Option A

16. Point out the error in the program. #include<stdio.h>#define MAX 128

int main(){ char mybuf[] = "India"; char yourbuf[] = "BIX"; char const *ptr = mybuf; *ptr = 'a'; ptr = yourbuf; return 0;}

A. Error: cannot convert ptr const value

B. Error: unknown pointer conversion

Page 47: Aptitude Questions and Answer

C. No error

D. None of above

Answer: Option A

Explanation:

Step 1: char mybuf[] = "India"; The variable mybuff is declared as an array of characters and initialized with string "India".

Step 2: char yourbuf[] = "BIX"; The variable yourbuf is declared as an array of characters and initialized with string "BIX".

Step 3: char const *ptr = mybuf; Here, ptr is a constant pointer, which points at a char.

The value at which ptr it points is a constant; it will be an error to modify the pointed character; There will not be any error to modify the pointer itself.

Step 4: *ptr = 'a'; Here, we are changing the value of ptr, this will result in the error "cannot modify a const object".

17. Declare the following statement?"An array of three pointers to chars".

A. char *ptr[3]();B. char *ptr[3];

C. char (*ptr[3])(); D. char **ptr[3];

Answer: Option B

18. What do the following declaration signify? int *f();

A. f is a pointer variable of function type.

B. f is a function returning pointer to an int.

C. f is a function pointer.

D. f is a simple declaration of pointer variable.

Answer: Option B

Page 48: Aptitude Questions and Answer

19. What will be the output of the program? #include<stdio.h>typedef unsigned long int uli;typedef uli u;

int main(){ uli a; u b = -1; a = -1; printf("%lu, %lu"a, b); return 0;}

A. 4343445454, 4343445454B. 4545455434, 4545455434

C. 4294967295, 4294967295 D. Garbage values

Answer: Option C

20. Can you use the fprintf() to display the output on the screen?A. Yes

B. No

Answer: Option A

Explanation:Do like this fprintf(stdout, "%s %d %f", str, i, a);