You are on page 1of 5

7.What is the output of the below C program ?

void main()
{
int arr[]={8,3,5,1,6};
int *ptr;
ptr=&arr[2];
printf("%d",ptr[-1]);
}
Compilation Error
4
3
2
Explanation:
ptr=&arr[2];// Will store the address of value 5 in ptr
printf("%d",ptr[-1]);
ptr[-1] --> *(address stored in ptr -1) which points to
nothing but the arr[1] which is 3. Hence, this option is correct

8.What is the output of the below C program ?


void main()
{
int a[]={10,20,30,40,50};
int *p;
p= (int*)((char *)a + sizeof(int));
printf("%d",*p);
}
20
30
40
10
Explanation:
Correct answer is 20. Consider
sizeof(int) as 4.
Now, (char *)a + 4 will point to the location current
address+4 bytes (Because of typecast of a as char
type)
p= (int*)((char *)a + sizeof(int));
Finally p is assigned address as (int *) so *p will point to
the 2nd integer location of array which is 4 bytes away from &a[0].
6.What is the output of the below C program ?
void main()
{
int i;

for(i=0;i<3;i++)
{
int i=100;
i--;
printf("%d..",i);
}
}
99..99..99..
0..1..2..
99..98..97..
100..100..100..
Explanation:
This is correct answer. While printing i the innermost one i.e., int i=100; is in scope. Hence, always
99 is printed.

3.What is the output of the below C program ?


#define putchar (c) printf("%c",c)
void main()
{
char d='c';
putchar (d);
}
Compilation error
99
c
Runtime error
Explanation:
There is no problem using keyword "putchar". We may define any valid identifier as a macro, even
though if it is a C keyword. The preprocessor doesn't know anything about keywords. So the output
c is displayed.
9.Which of the Nested For Loop will run faster ?
//First Nested For Loop
for(int i=0;i<100;i++)
for(int j=0;j<10;j++)
//some code
//Second Nested For Loop
for(int j=0;j<10;j++)
for(int i=0;i<100;i++)
//some code

Compiler and Hardware Dependent


Second For Loop
Both are Same.

First For Loop


Question 5 ster.
The Catch here is to look at the total comparisons each of
the nested For loop is running.
The First For loop does --> 10*101 + 1 comparisons.
The Second For loop does --> 100*11 + 1 comparisons.

1.A pointer 'p' is declared as follows in a C program.


void main()
{
int const * p;
int i[20];
p = i;
}
Which of the following is not allowed ?
*p++
All
++p
(*p)++
Question 6 Explanation:
int const * p; The above statement declares p a pointer to a constant integer. As p is initialized as
follows, int i[20]; p = i; That is, *p refers to the integer i[0]. Due to the deceleration of p (which, as
mentioned above, is int const * p;), statements, such as (*p)++; *p = 10; or even p[10] = 20; are not
valid. Such statements result in compile time errors.But remember p itself can be changed.
Statements, such as p++ ; are perfectly valid.
5.What is the output of the below C program ?
int calculate(int n)
{
if(n>0)
{
n=calculate(n-3)+calculate(n-1);
return(n);
}
return(1);
}
int main()
{
printf("%d",calculate(5));
return 0;
}
12

6
9
13
Explanation:
Correct Answer is 9.
2.What is the output of the below C program ?
void display_arr(int **ptr)
{
printf(" 0 -> %d 1 -> %d 2 -> %d 3 -> %d\n",ptr[0][0],ptr[0][1],ptr[0][2],ptr[0][3]);
}
void main()
{
int arr[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
int **int_arr;
int_arr = (int *)arr;
display_arr(int_arr);
int_arr++;
display_arr(int_arr);
}

Segmentation Fault
0 -> 0 1 -> 1 2 -> 2 3 -> 3 0 -> 8 1 -> 9 2 -> 10 3 -> 11
0 -> 0 1 -> 1 2 -> 2 3 -> 3 0 -> 4 1 -> 5 2 -> 6 3 -> 7
0 -> 4 1 -> 5 2 -> 6 3 -> 7 0 -> 8 1 -> 9 2 -> 10 3 -> 11
Explanation:
The pointer "int **int_arr;" declared for the two dimensional array here is not correct. Hence when
we try to pass this as a argument to the function "void display_arr(int **ptr)" it results in a
Segmentation Fault. Correct way of pointer deceleration is shown below,
void display_arr(int (*ptr)[4])
{
printf(" 0 -> %d 1 -> %d 2 -> %d 3 -> %d\n",ptr[0][0],ptr[0][1],ptr[0][2],ptr[0][3]);
}
void main()
{
int arr[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
int (*int_arr)[4]; //Is the proper declaration int_arr here is a pointer to an array of 4 integers
int_arr = arr;
display_arr(int_arr);
int_arr++;
display_arr(int_arr);
}

10.What is the output of the C program ?

#include
int main()
{
int a=0;
if (a = 2 )
{
printf("\nI am inside if part\n ",a);
}
else
{
printf("\nI am inside else part\n ",a);
}
return 0;
}
I am inside if part
Run Time Error
Compilation Error
I am inside else part
Explanation:
This is correct answer. I am inside if part.
Observe carefully in the above if check we have if (a = 2 ) and not if (a == 2 ).
So a will be assigned value 2. So the condition (a = 2)evaluates to 2. Anything > 0 is TRUE. Hence
we will enter in the first if check.

3.What is the output of the below C program ?


void main()
{
printf("%d",printf("XYZ\\"));
}
XYZ\4
XYZ\3
XYZ\\
1
Explanation:
This is correct answer --> "XYZ\4". Because, first printf("XYZ\\") is executed and it shall print
"XYZ\" (Always remember for printing one \ (backslash) we have to use \\). The successful
outcome of the first printf("XYZ\\") will return number of characters printed which is '4'.So the next
step executed is printf("%d",4); Hence the final output is XYZ\4

You might also like