Subex Papers: Sample Questions 5 - 6 of 19

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

Question number: 5

» Aptitude

Short Answer Question▾

Write in Short

What will be the output of the following code?

  1. #define max 10
  2. int main()
  3. {
  4.     printf("%d",++max);
  5. }

Explanation

  • Here max is preprocessor macro symbol which process first before the actual compilation. First preprocessor replace the symbol to its value in entire the program before the compilation. So in this program max will be replaced by 10 before compilation. Thus program will be converted like this:
  1. main()
  2. {
  3.     printf(“%d”,++10);
  4. }
  • This will generate error because we cannot assign constant value directly. Hence compiler will give error.

Question number: 6

» Aptitude

Short Answer Question▾

Write in Short

What will be the output of following code?

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     char *str = "Hello, world";
  5.     int i = sizeof(str);
  6.     for( ; i >= 0 ; i--)
  7.     printf("%c" , str[i]);
  8. }

Explanation

  • Here, str is a character pointer defined as Hello, world. It would mean the value at the address contained in str is character. Integer i is initialized by size of str variable. We can see that there are total 11 characters in str. So length of str is 11 and thus i is initialized with 11.
  • For loop has three parts: initialization, condition/test expression, increment/decrement
  • First any variable is initialized with some value. Then test expression is tested, if it satisfies then body of the loop gets executed. Then in last increment or decrement operation takes place.
  • Here, in for loop variable i is initialized as 11 and then whether 11 > =0 condition is checked. Here it is satisfied so str [11] will be printed on screen. Then, value of i gets decremented by 1 (i. e. i = 10) and again for loop gets executed and str [10] gets printed. It gets executed until condition is not satisfied.
  • So, in output we will get reverse string of str i. e. dlrow, olleH