You are on page 1of 40

REAL DIPLOMA & ENGINEERING CLASSES

ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

1. The following code for(;;) represents an infinite loop. It can be terminated by.
a) break
b) exit(0)
c) abort()
d) All of the mentioned
Answer:a
2. The correct syntax for running two variable for loop simultaneously is.
a) for (i = 0; i < n; i++)
for (j = 0; j < n; j += 5)
b) for (i = 0, j = 0;i < n, j < n; i++, j += 5)
c) for (i = 0; i < n;i++){}
for (j = 0; j < n;j += 5){}
d) None of the mentioned
Answer:b
3. Which for loop has range of similar indexes of 'i' used in for (i = 0;i < n; i++)?
a) for (i = n; i>0; i)
b) for (i = n; i >= 0; i)
c) for (i = n-1; i>0; i)
d) for (i = n-1; i>-1; i)
Answer:d
4. Which of the following cannot be used as LHS of the expression in for (exp1;exp2; exp3) ?
a) Variable
b) Function
c) typedef
d) macros
Answer:d
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
short i;
for (i = 1; i >= 0; i++)
printf("%d\n", i);
}

a) The control wont fall into the for loop


b) Numbers will be displayed until the signed limit of short and throw a runtime error
c) Numbers will be displayed until the signed limit of short and program will successfully
d) This program will get into an infinite loop and keep printing numbers with no errors
Answer:c

LEARNS FROM THE MASTERS

terminate

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

6. What is the output of this C code?


1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
int k = 0;
for (k)
printf("Hello");
}

a) Compile time error


b) hello
c) Nothing
d) Varies
Answer:a
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
int k = 0;
for (k < 3; k++)
printf("Hello");
}

a) Compile time error


b) Hello is printed thrice
c) Nothing
d) Varies
Answer:a
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
double k = 0;
for (k = 0.0; k < 3.0; k++)
printf("Hello");
}

a) Run time error


b) Hello is printed thrice
c) Hello is printed twice
d) Hello is printed infinitely
Answer:b

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

1. What is the output of this C code?


1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
double k = 0;
for (k = 0.0; k < 3.0; k++);
printf("%lf", k);
}

a) 2.000000
b) 4.000000
c) 3.000000
d) Run time error
Answer:c
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
int k;
for (k = -3; k < -5; k++)
printf("Hello");
}

a) Hello
b) Infinite hello
c) Run time error
d) Nothing
Answer:d
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int i = 0;
for (; ; ;)
printf("In for loop\n");
printf("After loop\n");
}

a) Compile time error


b) Infinite loop
c) After loop

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

d) Undefined behaviour
Answer:a
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int i = 0;
for (i++; i == 1; i = 2)
printf("In for loop ");
printf("After loop\n");
}

a) In for loop after loop


b) After loop
c) Compile time error
d) Undefined behaviour
Answer:a
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
int main()
{
int i = 0;
for (foo(); i == 1; i = 2)
printf("In for loop\n");
printf("After loop\n");
}
int foo()
{
return 1;
}

a) After loop
b) In for loop after loop
c) Compile time error
d) Infinite loop
Answer:a
6. What is the output of this C code?
1.
2.
3.
4.

#include <stdio.h>
int main()
{
int *p = NULL;

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

5.
6.
7.
8.

RAJNISH SRIVASTAV-9601253885

for (foo(); p; p = 0)
printf("In for loop\n");
printf("After loop\n");
}

a) In for loop after loop


b) Compile time error
c) Infinite loop
d) Depends on the value of NULL
Answer:b
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
int main()
{
for (int i = 0;i < 1; i++)
printf("In for loop\n");
}

a) Compile time error


b) In for loop
c) Depends on the standard compiler implements
d) Depends on the compiler
Answer:c
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
while ()
printf("In while loop ");
printf("After loop\n");
}

a) In while loop after loop


b) After loop
c) Compile time error
d) Infinite loop
Answer:c
2. What is the output of this C code?
1.
2.
3.

#include <stdio.h>
int main()
{

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

4.
5.
6.
7.
8.

RAJNISH SRIVASTAV-9601253885

do
printf("In while loop ");
while (0);
printf("After loop\n");
}

a) In while loop
b) In while loop
after loop
c) After loop
d) Infinite loop
Answer:b
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
int i = 0;
do {
i++;
printf("In while loop\n");
} while (i < 3);
}

a) In while loop
In while loop
In while loop
b) In while loop
In while loop
c) Depends on the compiler
d) Compile time error
Answer:a
4. How many times i value is checked in the below code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
int i = 0;
do {
i++;
printf("in while loop\n");
} while (i < 3);
}

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

a) 2
b) 3
c) 4
d) 1
Answer:b
5. How many times i value is checked in the below code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int i = 0;
while (i < 3)
i++;
printf("In while loop\n");
}

a) 2
b) 3
c) 4
d) 1
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
void main()
{
int i = 2;
do
{
printf("Hi");
} while (i < 2)
}

a) Compile time error


b) Hi Hi
c) Hi
d) Varies
Answer:a
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
void main()
{
int i = 0;
while (++i)
{

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

7.
8.
9.

RAJNISH SRIVASTAV-9601253885

printf("H");
}
}

a) H
b) H is printed infinite times
c) Compile time error
d) Varies
Answer:b
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
void main()
{
int i = 0;
do
{
printf("Hello");
} while (i != 0);
}

a) Nothing
b) H is printed infinite times
c) Hello
d) Run time error
Answer:c

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

1. What is the output of this C code?


1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
void main()
{
char *str = "";
do
{
printf("hello");
} while (str);
}

a) Nothing
b) Run time error
c) Varies
d) Hello is printed infinite times
Answer:d
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
void main()
{
int i = 0;
while (i < 10)
{
i++;
printf("hi\n");
} while (i < 8)
i++;
printf("hello\n");
}

a) Hi is printed 8 times, hello 7 times and then hi 2 times


b) Hi is printed 10 times, hello 7 times
c) Hi is printed once, hello 7 times
d) Hi is printed once, hello 7 times and then hi 2 times
Answer:d
3. Example of iteration in C.
a) for
b) while
c) do-while
d) All of the mentioned
Answer:d

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

4. Number of times while loop condition is tested is, i is initialized to 0 in both case.
while (i < n)
i++;
------------do
i++;
while (i <= n);
a) n, n
b) n, n+1
c) n+1, n
d) n+1, n+1
Answer:d
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int i = 0;
while (i = 0)
printf("True\n");
printf("False\n");
}

a) True (infinite time)


b) True (1 time) False
c) False
d) Compiler dependent
Answer:c
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
int main()
{
int i = 0, j = 0;
while (i < 5, j < 10)
{
i++;
j++;
}
printf("%d, %d\n", i, j);
}

a) 5, 5
b) 5, 10
c) 10, 10

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

d) Syntax error
Answer:c
7. Which loop is most suitable to first perform the operation and then test the condition?
a) for loop
b) while loop
c) do-while loop
d) None of the mentioned
Answer:c
1. The output of the code below is
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void main()
{
int x = 5;
if (x < 1)
printf("hello");
if (x == 5)
printf("hi");
else
printf("no");
}

a) hi
b) hello
c) no
d) None of the mentioned
Answer:a
2. The output of the code below is
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int x;
void main()
{
if (x)
printf("hi");
else
printf("how are u");
}

a) hi
b) how are you
c) Compile time error
d) None of the mentioned
Answer:b

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

3. Comment on the following code below


1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
int x = 5;
if (true);
printf("hello");
}

a) It will display hello


b) It will throw an error
c) Nothing will be displayed
d) Compiler dependent
Answer:b
4. The output of the code below is
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
void main()
{
int x = 0;
if (x == 0)
printf("hi");
else
printf("how are u");
printf("hello");
}

a) hi
b) how are you
c) hello
d) hihello
Answer:d
5. The output of the code below is
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
void main()
{
int x = 5;
if (x < 1);
printf("Hello");
}

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

a) Nothing
b) Run time error
c) Hello
d) Varies
Answer:c
6. The output of the code below is(when 1 is entered)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

#include <stdio.h>
void main()
{
double ch;
printf("enter a value btw 1 to 2:");
scanf("%lf", &ch);
switch (ch)
{
case 1:
printf("1");
break;
case 2:
printf("2");
break;
}
}

a) Compile time error


b) 1
c) 2
d) Varies
Answer:a
7. The output of the code below is(When 1 is entered)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
void main()
{
char *ch;
printf("enter a value btw 1 to 3:");
scanf("%s", ch);
switch (ch)
{
case "1":
printf("1");
break;
case "2":
printf("2");
break;

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

15.
16.

RAJNISH SRIVASTAV-9601253885

}
}

a) 1
b) 2
c) Compile time error
d) No Compile time error
Answer:c
8. When 1 is entered, The output of the code below is?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
void main()
{
int ch;
printf("enter a value btw 1 to 2:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("1\n");
default:
printf("2\n");
}
}

a) 1
b) 2
c) 1 2
d) Run time error
Answer:c
9. When 2 is entered, The output of the code below is?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

#include <stdio.h>
void main()
{
int ch;
printf("enter a value btw 1 to 2:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("1\n");
break;
printf("Hi");
default:

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

14.
15.
16.

RAJNISH SRIVASTAV-9601253885

printf("2\n");
}
}

a) 1
b) Hi 2
c) Run time error
d) 2
Answer:d
10. When 1 is entered, The output of the code below is?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

#include <stdio.h>
void main()
{
int ch;
printf("enter a value btw 1 to 2:");
scanf("%d", &ch);
switch (ch, ch + 1)
{
case 1:
printf("1\n");
break;
case 2:
printf("2");
break;
}
}

a) 1
b) 2
c) 3
d) Run time error
Answer:b
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
int x = 1;
if (x > 0)
printf("inside if\n");
else if (x > 0)
printf("inside elseif\n");
}

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

a) inside if
b) inside elseif
c) inside if
inside elseif
d) Compile time error
Answer:a
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
int x = 0;
if (x++)
printf("true\n");
else if (x == 1)
printf("false\n");
}

a) true
b) false
c) Compile time error
d) Undefined behaviour
Answer:b
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
int main()
{
int x = 0;
if (x == 1)
if (x == 0)
printf("inside if\n");
else
printf("inside else if\n");
else
printf("inside else\n");
}

a) inside if
b) inside else if
c) inside else
d) Compile time error
Answer:c
4. What is the output of this C code?

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

RAJNISH SRIVASTAV-9601253885

#include <stdio.h>
int main()
{
int x = 0;
if (x == 0)
printf("true, ");
else if (x = 10)
printf("false, ");
printf("%d\n", x);
}

a) false, 0
b) true, 0
c) true, 10
d) Compile time error
Answer:b
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
int main()
{
int x = 0;
if (x == 1)
if (x >= 0)
printf("true\n");
else
printf("false\n");
}

a) true
b) false
c) Depends on the compiler
d) No print statement
Answer:d
6. if (a == 1||b == 2){} can be written as:
a) if (a == 1)
if (b == 2){}
b) if (a == 1){}
if (b == 2){}
c) if (a == 1){}
else if (b == 2){}
d) None of the mentioned
Answer:d

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

7. Which of the following is an invalid if-else statement?


a) if (if (a == 1)){}
b) if (func1 (a)){}
c) if (a){}
d) if ((char) a){}
Answer:a
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
int a = 1;
if (a--)
printf("True");
if (a++)
printf("False");
}

a) True
b) False
c) True False
d) No Output
Answer:a
9. Comment on the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
int main()
{
int a = 1;
if (a)
printf("All is Well ");
printf("I am Well\n");
else
printf("I am not a River\n");
}

a) Output will be All is Well I am Well


b) Output will be I am Well I am not a River
c) Output will be I am Well
d) Compile time errors during compilation
Answer:d
10. What is the output of this C code?
1.

#include <stdio.h>

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

2.
3.
4.
5.
6.
7.
8.

RAJNISH SRIVASTAV-9601253885

int main()
{
if (printf("%d", printf(")))
printf("We are Happy");
else if (printf("1"))
printf("We are Sad");
}

a) 0We are Happy


b) 1We are Happy
c) 1We are Sad
d) Compile time error
Answer:d

1. What is the output of this C code?


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}

a) nullp nullq
b) Depends on the compiler
c) x nullq where x can be p or nullp depending on the value of NULL
d) p q
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int i = 10;
void *p = &i;
printf("%d\n", (int)*p);
return 0;
}

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

a) Compile time error


b) Segmentation fault/runtime crash
c) 10
d) Undefined behaviour
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}

a) Compile time error


b) Undefined behaviour
c) 10
d) 0.000000
Answer:d
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

#include <stdio.h>
int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int *j = (int*)malloc(sizeof(int));
*j = 10;
return j;
}

a) 10
b) Compile time error
c) Segmentation fault/runtime crash since pointer to local variable is returned
d) Undefined behaviour
Answer:a
5. What is the output of this C code?
1.
2.

#include <stdio.h>
int *f();

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

RAJNISH SRIVASTAV-9601253885

int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int j = 10;
return &j;
}

a) 10
b) Compile time error
c) Segmentation fault/runtime crash
d) Undefined behaviour
Answer:a
6. Comment on the following pointer declaration?
int *ptr, p;
a) ptr is a pointer to integer, p is not
b) ptr and p, both are pointers to integer
c) ptr is a pointer to integer, p may or may not be
d) ptr and p both are not pointers to integer
Answer:a
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}

a) 10,10
b) 10,11
c) 11,10
d) 11,11
Answer:d
8. Comment on the following?
const int *ptr;
a) You cannot change the value pointed by ptr
b) You cannot change the pointer ptr itself
c) Both (a) and (b)

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

d) You can change the pointer as well as the value pointed by it


Answer:a
1. Which is an indirection operator among the following?
a) &
b) *
c) ->
d) .
Answer:b
2. Which of the following does not initialize ptr to null (assuming variable declaration of a as int
a) int *ptr = &a;
b) int *ptr = &a &a;
c) int *ptr = a a;
d) All of the mentioned
Answer:a
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int x = 0;
void main()
{
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}

a) Same address
b) Different address
c) Compile time error
d) Varies
Answer:a
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int x = 0;
void main()
{
int *const ptr = &x;
printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);
}

LEARNS FROM THE MASTERS

a=0;?

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

a) 0 1
b) Compile time error
c) 0xbfd605e8 0xbfd605ec
d) 0xbfd605e8 0xbfd605e8
Answer:b
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
void main()
{
int x = 0;
int *ptr = &x;
printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);
}

a) 0xbfd605e8 0xbfd605ec
b) 0xbfd605e8 0cbfd60520
c) 0xbfd605e8 0xbfd605e9
d) Run time error
Answer:a
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
int x = 0;
int *ptr = &5;
printf("%p\n", ptr);
}

a) 5
b) Address of 5
c) Nothing
d) Compile time error
Answer:d
7. What is the output of this C code?
1.
2.
3.
4.
5.

#include <stdio.h>
void main()
{
int x = 0;
int *ptr = &x;

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

6.
7.

RAJNISH SRIVASTAV-9601253885

printf("%d\n", *ptr);
}

a) Address of x
b) Junk value
c) 0
d) Run time error
Answer:c
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void foo(int*);
int main()
{
int i = 10;
foo((&i)++);
}
void foo(int *p)
{
printf("%d\n", *p);
}

a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault/code crash
Answer:c
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void foo(int*);
int main()
{
int i = 10, *p = &i;
foo(p++);
}
void foo(int *p)
{
printf("%d\n", *p);
}

a) 10
b) Some garbage value
c) Compile time error

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

d) Segmentation fault
Answer:a
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void foo(float *);
int main()
{
int i = 10, *p = &i;
foo(&i);
}
void foo(float *p)
{
printf("%f\n", *p);
}

a) 10.000000
b) 0.000000
c) Compile time error
d) Undefined behaviour
Answer:b
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

#include <stdio.h>
int main()
{
int i = 97, *p = &i;
foo(&i);
printf("%d ", *p);
}
void foo(int *p)
{
int j = 2;
p = &j;
printf("%d ", *p);
}

a) 2 97
b) 2 2
c) Compile time error
d) Segmentation fault/code crash
Answer:a
5. What is the output of this C code?

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

RAJNISH SRIVASTAV-9601253885

#include <stdio.h>
int main()
{
int i = 97, *p = &i;
foo(&p);
printf("%d ", *p);
return 0;
}
void foo(int **p)
{
int j = 2;
*p = &j;
printf("%d ", **p);
}

a) 2 2
b) 2 97
c) Undefined behaviour
d) Segmentation fault/code crash
Answer:a
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
int main()
{
int i = 11;
int *p = &i;
foo(&p);
printf("%d ", *p);
}
void foo(int *const *p)
{
int j = 10;
*p = &j;
printf("%d ", **p);
}

a) Compile time error


b) 10 10
c) Undefined behaviour
d) 10 11
Answer:a
7. What is the output of this C code?
1.

#include <stdio.h>

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

RAJNISH SRIVASTAV-9601253885

int main()
{
int i = 10;
int *p = &i;
foo(&p);
printf("%d ", *p);
printf("%d ", *p);
}
void foo(int **const p)
{
int j = 11;
*p = &j;
printf("%d ", **p);
}

a) 11 11 11
b) 11 11 Undefined-value
c) Compile time error
d) Segmentation fault/code-crash
Answer:b
8. What is the output of the code below?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
int main()
{
int i = 10;
int *const p = &i;
foo(&p);
printf("%d\n", *p);
}
void foo(int **p)
{
int j = 11;
*p = &j;
printf("%d\n", **p);
}

a) 11 11
b) Undefined behaviour
c) Compile time error
d) Segmentation fault/code-crash
Answer:a
9. Which of the following are correct syntaxes to send an array as a parameter to function:
a) func(&array);
b) func(array);

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

c) func(*array);
d) func(array[size]);
Answer:a & b
1. Which of the following can never be sent by call-by-value?
a) Variable
b) Array
c) Structures
d) Both (b) and (c)
Answer:b
2. Which type of variables can have same name in different function:
a) global variables
b) static variables
c) Function arguments
d) Both (b) and (c)
Answer:d
3. Arguments that take input by user before running a program are called?
a) main function arguments
b) main arguments
c) Command-Line arguments
d) Parameterized arguments
Answer:c
4. The maximum number of arguments that can be passed in a single function are_____________
a) 127
b) 253
c) 361
d) No limits in number of arguments
Answer:b
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void m(int *p, int *q)
{
int temp = *p; *p = *q; *q = temp;
}
void main()
{
int a = 6, b = 5;
m(&a, &b);
printf("%d %d\n", a, b);
}

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

a) 5 6
b) 6 5
c) 5 5
d) 6 6
Answer:a
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
void m(int *p)
{
int i = 0;
for(i = 0;i < 5; i++)
printf("%d\t", p[i]);
}
void main()
{
int a[5] = {6, 5, 3};
m(&a);
}

a) 0 0 0 0 0
b) 6 5 3 0 0
c) Run time error
d) 6 5 3 junk junk
Answer:b
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

#include <stdio.h>
void m(int p, int q)
{
int temp = p;
p = q;
q = temp;
}
void main()
{
int a = 6, b = 5;
m(a, b);
printf("%d %d\n", a, b);
}

a) 5 6
b) 5 5
c) 6 5

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

d) 6 6
Answer:c
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
void m(int p, int q)
{
printf("%d %d\n", p, q);
}
void main()
{
int a = 6, b = 5;
m(a);
}

a) 6
b) 6 5
c) 6 junk value
d) Compile time error
Answer:d
9. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void m(int p)
{
printf("%d\n", p);
}
void main()
{
int a = 6, b = 5;
m(a, b);
printf("%d %d\n", a, b);
}

a) 6
b) 6 5
c) 6 junk value
d) Compile time error
Answer:d
1. What is the output of this C code?
1.
2.
3.

#include <stdio.h>
void main()
{

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

4.
5.
6.
7.

RAJNISH SRIVASTAV-9601253885

int a[3] = {1, 2, 3};


int *p = a;
printf("%p\t%p", p, a);
}

a) Same address is printed.


b) Different address is printed.
c) Compile time error
d) Nothing
Answer:a
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s = "hello";
char *p = s;
printf("%p\t%p", p, s);
}

a) Different address is printed


b) Same address is printed
c) Run time error
d) Nothing
Answer:b
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s;
printf("%c\t%c", p[0], s[1]);
}

a) Run time error


b) h h
c) h e
d) h l
Answer:c
4. What is the output of this C code?
1.
2.

#include <stdio.h>
void main()

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

3.
4.
5.
6.
7.

RAJNISH SRIVASTAV-9601253885

{
char *s= "hello";
char *p = s;
printf("%c\t%c", *(p + 3), s[1]);
}

a) h e
b) l l
c) l o
d) l e
Answer:d
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s;
printf("%c\t%c", 1[p], s[1]);
}

a) h h
b) Run time error
c) l l
d) e e
Answer:d
6. What is the output of the code given below?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
void foo( int[] );
int main()
{
int ary[4] = {1, 2, 3, 4};
foo(ary);
printf("%d ", ary[0]);
}
void foo(int p[4])
{
int i = 10;
p = &i;
printf("%d ", p[0]);
}

a) 10 10
b) Compile time error

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

c) 10 1
d) Undefined behaviour
Answer:c
7. What is the output of the code given below?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
int *p = ary + 3;
printf("%d\n", p[-2]);
}

a) 1
b) 2
c) Compile time error
d) Some garbage value
Answer:b
8. What is the output of the code given below?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
int *p = ary + 3;
printf("%d %d\n", p[-2], ary[*p]);
}

a) 2 3
b) Compile time error
c) 2 4
d) 2 somegarbagevalue
Answer:d
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
printf("%d\n", *ary);
}

a) 1
b) Compile time error

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

c) Some garbage value


d) Undefined variable
Answer:a
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
const int ary[4] = {1, 2, 3, 4};
int *p;
p = ary + 3;
*p = 5;
printf("%d\n", ary[3]);
}

a) 4
b) 5
c) Compile time error
d) 3
Answer:b
3. Different ways to initialize an array with all elements as zero are
a) int array[5] = {};
b) int array[5] = {0};
c) int a = 0, b = 0, c = 0;
int array[5] = {a, b, c};
d) All of the mentioned
Answer:d
4. The elements in the array of the following code are
int array[5] = {5};
a) 5, 5, 5, 5, 5
b) 5, 0, 0, 0, 0
c) 5, (garbage), (garbage), (garbage), (garbage)
d) (garbage), (garbage), (garbage), (garbage), 5
Answer:b
5. Which of the following declaration is illegal?
a) int a = 0, b = 1, c = 2;
int array[3] = {a, b, c};
b) int size = 3;
int array[size];
c) int size = 3;
int array[size] = {1, 2, 3};
d) All of the mentioned
Answer:c

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

6. An array of similar data types which themselves are collection of dissimilar data type are
a) Linked Lists
b) Trees
c) Array of Structure
d) All of the mentioned
Answer:c
7. Comment on an array of void data type:
a) It can store any data-type
b) It only stores element of similar data type to first element
c) It acquires the data type with the highest precision in it
d) You cannot have an array of void data type
Answer:d
8. What is the output of the code given below?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
int p[4];
p = ary;
printf("%d\n", p[1]);
}

a) 1
b) Compile time error
c) Undefined behaviour
d) 2
Answer:b
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
double *ptr = (double *)100;
ptr = ptr + 2;
printf("%u", ptr);
}

a) 102
b) 104
c) 108
d) 116
Answer:d

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

2. Comment on the output of this C code?


1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
int *p = (int *)2;
int *q = (int *)3;
printf("%d", p + q);
}

a) 2
b) 3
c) 5
d) Compile time error
Answer:d
3. Which of the following operand can be applied to pointers p and q?
(Assuming initialization as int *a = (int *)2; int *b = (int *)3;)
a) a + b
b) a b
c) a * b
d) a / b
Answer:b
4. What is the size of *ptr in a 32-bit machine, (assuming initialization as int *ptr = 10;)?
a) 1
b) 2
c) 4
d) 8
Answer:c
5. Which of following logical operation can be applied to pointers?
(Assuming initialization int *a = 2; int *b = 3;)
a) a | b
b) a ^ b
c) a & b
d) None of the mentioned
Answer:d
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
void main()
{
char *s = "hello";
char *p = s;
printf("%c\t%c", *(p + 1), s[1]);

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

7.

RAJNISH SRIVASTAV-9601253885

a) h e
b) e l
c) h h
d) e e
Answer:d
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s = "hello";
char *p = s;
printf("%c\t%c", *p, s[1]);
}

a) e h
b) Compile time error
c) h h
d) h e
Answer:d
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
void main()
{
char *s = "hello";
char *n = "cjn";
char *p = s + n;
printf("%c\t%c", *p, s[1]);
}

a) h e
b) Compile time error
c) c o
d) h n
Answer:b
1. What is the output of this C code?
1.
2.
3.
4.

#include <stdio.h>
void main()
{
char *s = "hello";

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

5.
6.
7.

RAJNISH SRIVASTAV-9601253885

char *p = s * 3;
printf("%c\t%c", *p, s[1]);
}

a) h e
b) l e
c) Compile time error
d) l h
Answer:c
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s + 2;
printf("%c\t%c", *p, s[1]);
}

a) l e
b) h e
c) l l
d) h l
Answer:a
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
int main()
{
void *p;
int a[4] = {1, 2, 3, 8};
p = &a[3];
int *ptr = &a[2];
int n = p - ptr;
printf("%d\n", n);
}

a) 1
b) Compile time error
c) Segmentation fault
d) 4
Answer:b
4. What is the output of this C code?

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

RAJNISH SRIVASTAV-9601253885

#include <stdio.h>
int main()
{
void *p;
int a[4] = {1, 2, 3, 4};
p = &a[3];
int *ptr = &a[2];
int n = (int*)p - ptr;
printf("%d\n", n);
}

a) 1
b) Compile time error
c) Segmentation fault
d) 4
Answer:a
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int a[4] = {1, 2, 3, 4};
int b[4] = {1, 2, 3, 4};
int n = &b[3] - &a[2];
printf("%d\n", n);
}

a) -3
b) 5
c) 4
d) Compile time error
Answer:a
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
int a[4] = {1, 2, 3, 4};
int *p = &a[1];
int *ptr = &a[2];
ptr = ptr * 1;
printf("%d\n", *ptr);
}

LEARNS FROM THE MASTERS

REAL DIPLOMA & ENGINEERING CLASSES


ADVANCE PROGRAMMING IN C

RAJNISH SRIVASTAV-9601253885

a) 2
b) 1
c) Compile time error
d) Undefined behaviour
Answer:c
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
int a[4] = {1, 2, 3, 4};
int *ptr = &a[2];
float n = 1;
ptr = ptr + n;
printf("%d\n", *ptr);
}

a) 4
b) 3
c) Compile time error
d) Undefined behaviour
Answer:c
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
int main()
{
int a[4] = {1, 2, 3, 4};
void *p = &a[1];
void *ptr = &a[2];
int n = 1;
n = ptr - p;
printf("%d\n", n);
}

a) 1
b) 4
c) Compile time error
d) Depends on the compiler
Answer:b

LEARNS FROM THE MASTERS

You might also like