# Grapecity C + + Questions: C++ Questions Part II

Examrace Placement Series prepares you for the toughest placement exams to top companies.

1. What is the ouptut in the following program main () { char c = -64; int i = -32 unsigned int u = -16; if (c>i) { printf ( “pass1,” ); if cprintf ( “pass2” ); else printf ( “Fail2” ); } else printf ( “Fail1); if iprintf (” pass2 “); else printf (” Fail2") }

1. Pass1, Pass2

2. Pass1, Fail2

3. Fail1, Pass2

4. Fail1, Fail2

5. None of these

Ans (c).

2. What will the following program do? void main () { int i; char a[] = “String” char * p = “New Sring” char * Temp; Temp = a; a = malloc (strlen (p) + 1); strcpy (a, p);//Line number: 9//p = malloc (strlen (Temp) + 1); strcpy (p, Temp); rintf ( “(%s, %s)” a, p); free (p); free (a); }//Line number 15//

1. Swap contents of p & a and print: (New string, string)

2. Generate compilation error in line number 8

3. Generate compilation error in line number 5

4. Generate compilation error in line number 7

5. Generate compilation error in line number 1

Ans (b).

3. In the following code segment what will be the result of the function, value of x, value of y { unsigned int x = -1; int y; y = ~0; if (x = = y) printf ( “same” ); else printf ( “not same” ); }

1. same, MAXINT, 1

2. not same, MAXINT, MAXINT

3. same, MAXUNIT, 1

4. same, MAXUNIT, MAXUNIT

5. not same, MAXINT, MAXUNIT

Ans (a).

4. What will be the result of the following program? char * gxxx () { static char xxx[1024]; return xxx; } main () { char * g = “string” strcpy (gxxx (), g); g = gxxx (); strcpy (g, “oldstring” ); printf ( “The string is: %s” gxxx () ); }

1. The string is: String

2. The string is: Oldstring

3. Run time error/Core dump

4. Syntax error during compilation

5. None of these

Ans (b).

5. Find the output for the following C program main () { char * p1 = “Name” char * p2; p2 = (char * ) malloc (20); while ( * p2 + + = * p1 + + ); printf ( “%s\n” p2); } Ans. An empty string

6. Find the output for the following C program main () { int x = 20, y = 35; x = y + + + x + +; y = + + y + + + x; rintf ( “%d %d\n” x, y); } Ans. 57 94

7. Find the output for the following C program main () { int x = 5; printf ( “%d %d %d\n” x, x<<2, x>>2); } Ans. 5 20 1

8. Find the output for the following C program #define swap1 (a, b) a = a + b; b = a-b; a = a-b; main () { int x = 5, y = 10; swap1 (x, y); printf ( “%d %d\n” x, y); swap2 (x, y); printf ( “%d %d\n” x, y); } int swap2 (int a, int b) { int temp; temp = a; b = a; a = temp; return; } Ans. 10 5

9. Find the output for the following C program main () { char * ptr = “Ramco Systems” ( * ptr); + +; printf ( “%s\n” ptr); ptr + +; printf ( “%s\n” ptr); } Ans. Samco Systems

10. Find the output for the following C program #include main () { char s1[] = “Ramco” char s2[] = “Systems” s1 = s2; printf ( “%s” s1); } Ans. Compilation error giving it cannot be an modifiable ‘lvalue’

11. Find the output for the following C program #include main () { char * p1; char * p2; p1 = (char * ) malloc (25); p2 = (char * ) malloc (25); strcpy (p1, “Ramco” ); strcpy (p2, “Systems” ); strcat (p1, p2); printf ( “%s” p1); } Ans. RamcoSystems

12. Find the output for the following C program given that The following variable is available in file1. c static int average_float; Ans. All the functions in the file1. c can access the variable

13. Find the output for the following C program # define TRUE 0 some code while (TRUE) { some code } Ans. This won't go into the loop as TRUE is defined as 0

14. struct list { int x; struct list * next; } * head; the struct head. x = 100 Is the above assignment to pointer is correct or wrong? Ans. Wrong

15. What is the output of the following? int i; i = 1; i = i + 2 * i + +; printf (%d, i); Ans. 4

16. FILE * fp1, * fp2; fp1 = fopen ( “one” “w” ) fp2 = fopen ( “one” “w” ) fputc ( ‘A’ fp1) fputc ( ‘B’ fp2) fclose (fp1) fclose (fp2) Find the Error, If Any? Ans. No error. But It will over writes on same file.

17. What are the output (s) for the following?

18. #include char * f () { char * s = malloc (8); strcpy (s, “goodbye” ); } main () { char * f (); printf ( “%c” * f () = ‘A’ ); }

19. #define MAN (x, y) (x) > (y) (x)? (y) { int i = 10; j = 5; k = 0; k = MAX (i + +, + + j); printf (%d %d %d %d, i, j, k); } Ans. 10 5 0

void main () { int i = 7; printf ( “%d” i + + * i + + ); }

Ans: 56