Some Questions

<quiz display="simple">

{Consider the following program: <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   int a[10][20][30]={0};
   printf("%ld",&a+1 - &a);
   return 0;

} </syntaxhighlight> What is the output of this program? |type="{}" /} { 1 }


{Consider the following program: <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   int a[10][20][30] = {0};
   int *b = a;
   int *c = a+1;
   printf("%ld", c-b);
   return 0;

} </syntaxhighlight> What is the output of this program? (You may ignore compiler warnings) |type="{}" /} { 600 }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <stdlib.h>

int* fun();

int main() {

   int *a = fun();
   printf("%d",*a);
   return 0;

} int* fun() {

   int *a =(int*) malloc(sizeof(int));
   *a = 10;
   return a;

} </syntaxhighlight> |type="{}" /} { 10 }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   int *a = fun();
   printf("%d",a);
   return 0;

} int fun() {

   int a = 10;
   return a;

} </syntaxhighlight> |type="{}" /} { 10 }


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <string.h>

int main() {

   char string[] = "Hello";
   printf("%zu %zu",sizeof(string),strlen(string));
   return 0;

} </syntaxhighlight> |type="{}" /} { 6 5 }


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   float a = 0.5;
   if(a == 0.5)
       printf("Yes");
   else
       printf("No");
   return 0;

} </syntaxhighlight> |type="{}" /} { Yes }


{What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <string.h>

void foo(char *);

int main() {

   char a[100] = {0};
   printf("%zu %zu",sizeof(a),strlen(a));
   return 0;

} </syntaxhighlight> |type="{}" /} { 100 0 }



{What is the output of the following program? (Assume input is 10) <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   int a;
   printf("%d",scanf("%d",&a));
   return 0;

} </syntaxhighlight> |type="{}" /} { 1 }


{If the binary equivalent of 5.375 in normalised form is 01000000 10101100 00000000 00000000, what will be the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <math.h>

int main() {

   float a=5.375;
   char *p;
   int i;
   p = (char*)&a;
   for(i=0; i<2; i++)
       printf("%x", (unsigned char)(p[i]^p[3-i]));
   return 0;

} </syntaxhighlight> |type="{}" /} { 40ac }


{ What is the output of the following program? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   char str[] = {'a','b','c','\0'};
   str[0] -= 32;
   printf("%s",str);
   return 0;

} </syntaxhighlight> |type="{}" /} { Abc }

</quiz>

Questions from Datatypes

<quiz display="simple"> {Consider an implementation where int is 4 bytes and long int is 8 bytes. Which of the following initializations are correct? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  long int a = 0x7fffffff * 0x7ffffff;
  long int b = 0x7ffffffff * 0x7ffffff;
  long int c = 0x7fffffff * 0x7fffffff;
  long int d = 0x7fffffff * 0x7fffffffl;
  printf("a = %ld, b = %ld, c = %ld, d = %ld\n", a, b, c, d);
  return 0;

} </syntaxhighlight> |type="()" /} -All are correct -a, c, d +b, d -a, d ||a and c choices cause integer overflow. Even though long int can hold 8 bytes as given in the question, the operands are of 4 bytes only and hence the result is also 4 bytes.
In b choice, 0x7ffffffff is taking more than 4 bytes and hence is considered a long int. So, the next operand is implicitly typecasted to long int and the result is also calculated as long int. Hence there'll be no overflow.
In d choice, by adding l at the end, we force the compiler to use long int operand and hence the operations will be done using 8 byte operands and there will be no overflow.

{Consider an implementation where int is 4 bytes and long int is 8 bytes. What will be the output of the following code? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  int i = 0;
  size_t a = sizeof i, b = sizeof (long);
  printf("a = %zd, b = %zd\n", a, b); //If %zd is given the compiler will automatically give it the correct type whether short, long or normal. This is useful for special data types like size_t whose size is implementation specific
  return 0;

} </syntaxhighlight> |type="()" /} -Compile error -Runtime error -a = 4, b = 4 +a = 4, b = 8 ||sizeof is an operator and hence we don't need a parentheses for giving variables to sizeof. So, sizeof i will return 4 which is the size of int and sizeof(long) will return 8


{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  unsigned int a = 5;
  if(a > -1)
     printf("5 is > -1\n");
  return 0;

} </syntaxhighlight> |type="()" /}

-5 is > -1 +No output -Compile Error -Runtime Error ||When an operation involves different data types the lower ranked one is promoted to higher ranked one. So, when we compare a signed int with an unsigned int, the signed int will be promoted to unsigned (unsigned has higher rank than signed).
a > -1
will turn to
00...101 > 11...111
and will evaluate to false

If a was declared as signed, then the if condition would have behaved as expected.
(Ideally an unsigned integer would never be compared with a negative number in real world and so do in programs)

{What will be printed by the following code? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <string.h>

int main() {

  char buff[255] = "abc\

pee";

  printf("%s", buff);
  return 0;

} </syntaxhighlight> |type="()" /}

+
abcpee -
abc
pee -
Compile error -
abc
ee

||\ followed by a newline will make the compiler escape both the characters. So, this is used to write long lines of code across multiple lines for enhancing readability. So,
"abc\
pee";
is equivalent to
"abcpee";

{What will be printed by the following code? <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include<string.h>

int main() {

   char buff[] = "abc" "hello";
   printf("%zd\n", strlen(buff));
   return 0;

} </syntaxhighlight> |type="()" /}

+8 -9 -10 -Compile Error ||Compiler will append any continuous string literals given inside "" and make a single string literal ignoring the space(s) between them. So, "abc" "hello" will become "abchello" and its length is 8


{How can you print the following sentence exactly as it is by changing the assignment to buff? "Hello\\" "World\\" <syntaxhighlight lang="c">

  1. include <stdio.h>
  2. include <string.h>

int main() {

   char buff[255] = "\0";
   printf("%s", buff);
   return 0;

} </syntaxhighlight> |type="()" /}

-char buff[255] = "\"Hello/\/\\" \"World/\/\\""; +char buff[255] = "\"Hello\\\\\" \"World\\\\\""; -char buff[255] = ""Hello\\" "World\\""; -char buff[255] = ""Hello\\\\\" "World\\\\""; ||To put " in a string we have to escape it with \. i.e., use \" instead of ". For '\' also we do the same \\ instead of \

{What will be the output of the following code? (Assume a 64 bit machine/compiler) <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

   char *p = "Hello World";
   char q[] = "Hello World";
   printf("%zd %zd", sizeof p, sizeof *p);
   printf("\n");
   printf("%zd %zd", sizeof q, sizeof *q);
   return 0;

} </syntaxhighlight> |type="()" /}

+
8 1
12 1 -
12 1
12 1 -
Compile error -
11 1
12 1 ||p is a char pointer and all pointers on 64 bit platform takes 64bits -> 8 bytes
*p is a char and size of char is 1 byte
q is an array and sizeof array is the no. of elements in the array X sizeof an element
= 12 (including \0 which is added by compiler at the end of all string literals) * 1   = 12 bytes ||*q is a char and sizeof char is 1 byte

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include <stdio.h>

int main() {

  {
     char a = 5;
     int b = 5;
     if(a == b)          
        printf("char and int compared equal\n");    }
  {
     int a = 5;
     long int b = 5;

if(a == b)

        printf("int and long compared equal\n");
  }
  {
     float a = 5.0;
     double b = 5.0;
     if(a == b)
        printf("float and double compared equal\n");
  }
  {
     float a = 5.2;
     double b = 5.2;
     if(a == b)
        printf("float and double again compared equal\n");
  }
  {
     float a = 5.2;
     if(a == 5.2)
        printf("float compared equal with constant\n");
  }
  {
     double a = 5.2;
     if(a == 5.2)
        printf("double compared equal with constant\n");
  }
  return 0;

} </syntaxhighlight> |type="()" /} +
char and int compared equal
int and long compare equal
float and double compared equal
double compared equal with constant

-
char and int compared equal
int and long compare equal
double compared equal with constant

-
char and int compared equal
int and long compare equal
float and double again compared equal
double compared equal with constant

-
char and int compared equal
int and long compare equal
float and double compared equal

||
When we compare an int and a char, the char will be promoted to int by adding 0s to the left. So, signed integers less than 128 (the maximum value representable by a signed char is 127) will compare equal with char.

||When we compare a long and an int, int is promoted to long by adding 0s to the left. So, any long number than can be represented by an int will compare equal with int.


||Like above , float will be promoted to double. If the float number is exactly representable (without any approximation) within a float, then it'll compare equal with its double counterpart.

||Here, 5.2 cannot be exactly representable using a float. Hence, its double version will have more accuracy and won't compare equal with float.

||By default all real values are taken as double. If we want to take as float we have to use

if(a == 5.2f)
instead of

if(a == 5.2)

</quiz>


Questions from Pointers

<quiz display="simple"> {What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a = 5;
 int* p = &a;
 printf("%d", ++*p);

} </syntaxhighlight> |type="{}" /} { 6 } ||p is pointing to the address of a. *p will return the content of a which is 5 and ++ will increment it to 6.


{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 char a[] = "Hello World";
 char* p = &a;
 printf("%s", p+2 );

} </syntaxhighlight> |type="()" /} -Compiler Error -World +llo World -Runtime Error

||Since p is a char pointer p+2 will add 2 to p (since sizeof(char) is 1). So, p+2 will be pointing to the string "llo World".

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a;
 int* p = &a;
 printf("%zu", sizeof( *(char*)p  ));

} </syntaxhighlight> |type="()" /}

+1 -2 -4 -Compile Error

||p is typecasted to char pointer and then dereferenced. So, returned type will be char and sizeof(char) is 1.

{Is the following code legal? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a = 1;
 if((char*)  &a)
 {
   printf("My machine is little endian");
 }
 else
 {
    printf("My machine is big endian\n");
 }

} </syntaxhighlight> |type="()" /} +Yes -No ||On a little endian machine the lower address will contain the least significant byte. Suppose a is stored at address 1000 and contains 1, then character at 1000 will be 1, if the machine is little endian.

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int *a = (int*) 1;
 printf("%d", a);

} </syntaxhighlight> |type="()" /} -Garbage value +1 -0 -Compile error -Segmentation fault ||Assigning int values to pointer variable is possible. Only when we dereference the variable using *, we get a segmentation fault.

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a = 1, *p, **pp;
 p = &a;
 pp = p;
 printf("%d", **pp);

} </syntaxhighlight> |type="()" /} -Garbage value -1 -Compile error +Segmentation fault ||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a, but **p will use 1 as an address and tries to access the memory location 1, giving segmentation fault.

{What will be the output of the following code? <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

 int a = 1, *p, **pp;
 p = &a;
 pp = p;
 printf("%d", *pp);

} </syntaxhighlight> |type="()" /} +1 -Garbage value -Compile error -Segmentation fault ||Here, p is having address of a and the same is copied to pp. So, *pp will give 1 which is contained in a.


{Assuming a little endian machine, what will be the output of the following program? <syntaxhighlight lang="c">

  1. include<stdio.h>

fun(int a) {

 char *arr[] = {"0000", "0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
 unsigned char* p = (unsigned char*) &a ;
 p+=3;
 int i;
 for(i = 0; i < sizeof a; i++)
 {
   int d = (*p)>>4; 
   printf("%s", arr[d]);
   d = (*p) & 0xf;
   printf("%s ", arr[d]);
   p--;
 }

}

int main() {

int a;
scanf("%d", &a);
fun(a);

} </syntaxhighlight> |type="()" /} +Print the binary of the input number -Compile error -Runtime error -Compiler dependent output ||The code is printing the binary equivalent of the input number. Suppose a is stored starting from address 1000. Since, we assume a little endian machine, the LSB of a will be stored at address 1000 and MSB will be stored at address 1003. So, we make a char pointer to 1003 and take out the MSB. Then using shift operator we get the most significant 4 bits from it and then the lest significant 4 bits. We repeat the same for the other three bytes of a. </quiz>


Some codes

1. What is the following function doing? <syntaxhighlight lang="c"> int foo(int n) {

   int sum = 0;
   while(n > 0)
   {
       n = n & n-1;
       sum++;
   }
   return sum;

} </syntaxhighlight> Ans:


2. What is the following function doing? <syntaxhighlight lang="c"> int foo(int a, int b) {

   int c = a, d = b;
   while(a != b)
   {
       if(a < b)
           a = a+c;
       else
           b = b+d;
   }
   return a;

} </syntaxhighlight> Ans:


3. What is the following function doing? <syntaxhighlight lang="c"> int foo( int a, int b) {

   int c = a-b;
   c = c&(0x80000000);
   return (!c)*a +(!!c)*b;

} </syntaxhighlight> Ans:


4. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned a, unsigned b) {

   int i;
   unsigned  j = 0;
   for(i = 0; i < 32; i++)
   {
       j <<= 1;
       j += !!(a & 0x80000000);
       a <<= 1;
       if(j >=b)
       {
           j -= b;
           a++;
       }
   }
   return a;

} </syntaxhighlight> Ans:


5. What is the following function doing? <syntaxhighlight lang="c"> unsigned fun(unsigned int a) {

   unsigned int i, x = 0, y = 0, z = 0;
   for(i = 0; i < 16; i++)
   {
      	y <<= 2;
      	y += !!(a & 0x80000000) << 1;
      	y += !!(a & 0x40000000);
      	a <<= 2;
      	x = x + (x&1);
      	x <<= 1;
      	z <<= 1;
      	if(x + 1 <= y)
      	{
           x++;
           z++;

y-=x;

       }
   }
   return z;

} </syntaxhighlight> Ans:



6. Write the code to dynamically allocate a 2-D array of size m x n.

Ans:



7. Declare a pointer to a function accepting an integer and returning void.

Ans:


8. Write the condition so that the below code outputs <math>\unicode{x201C}</math>Hello World<math>\unicode{x201D}</math>. <syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

   if(<condition>)
   {

printf("Hello ");

   }
   else
   {

printf("World\n");

   }
   return 0;

} </syntaxhighlight> Ans:


9. Write a one line code to check if a number is a power of 2.

Ans:


10. Write a one line code to invert the last four bits of an integer.

Ans:





blog comments powered by Disqus



This work is licensed under the CC By-SA 3.0 , without all the cruft that would otherwise be put at the bottom of the page.

Sister Sites: GATE CSE Wiki, GATE CSE, Aptitude Overflow