Cprogramming
Cprogramming
green=simple
Orange=difficult
#include <stdio.h>
void main()
{
int x = 0;
if (x = 0)
printf("Its zero\n");
else
printf("Its not zero\n");
}
a) Its not zero
b) Its zero
c) Run time error
d) None
View Answer
Answer: a
#include <stdio.h>
void main()
{
int k = 8;
int x = 0 == 1 && k++;
printf("%d%d\n", x, k);
}
a) 0 9
b) 0 8
c) 1 8
d) 1 9
View Answer
Answer: b
Explanation: None.
#include <stdio.h>
void main()
{
char a = 'a';
int x = (a % 10)++;
printf("%d\n", x);
}
a) 6
b) Junk value
c) Compile time error
d) 7
View Answer
Answer: c
Explanation: None.
#include <stdio.h>
void main()
{
1 < 2 ? return 1: return 2;
}
a) returns 1
b) returns 2
c) Varies
d) Compile time error
View Answer
Answer: d
Explanation: None.
#include <stdio.h>
void main()
{
int x = 1, y = 0, z = 5;
int a = x && y || z++;
printf("%d", z);
}
a) 6
b) 5
c) 0
d) Varies
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
void main()
{
int x = 1, y = 0, z = 5;
int a = x && y && z++;
printf("%d", z);
}
a) 6
b) 5
c) 0
d) Varies
View Answer
Answer: b
Explanation: None.
#include <stdio.h>
int main()
{
int x = 1, y = 0, z = 3;
x > y ? printf("%d", z) : return z;
}
a) 3
b) 1
c) Compile time error
d) Run time error
View Answer
Answer: c
Explanation: None.
#include <stdio.h>
void main()
{
int x = 1, z = 3;
int y = x << 3;
printf(" %d\n", y);
}
a) -2147483648
b) -1
c) Run time error
d) 8
View Answer
Answer: d
Explanation: None.
#include <stdio.h>
void main()
{
int x = 0, y = 2, z = 3;
int a = x & y | z;
printf("%d", a);
}
a) 3
b) 0
c) 2
d) Run time error
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
int main()
{
int i = 0, j = 0;
if (i && (j = i + 10))
//do something
;
}
a) 0
b) 10
c) Depends on the compiler
d) Depends on language standard
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
int main()
{
int i = 10, j = 0;
if (i || (j = i + 10))
//do something
;
}
a) 0
b) 20
c) Compile time error
d) Depends on language standard
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
int main()
{
int i = 1;
if (i++ && (i == 1))
printf("Yes\n");
else
printf("No\n");
}
a) Yes
b) No
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: b
Explanation: None.
#include <stdio.h>
int main()
{
int x = 2, y = 0;
int z = (y++) ? y == 1 && x : 0;
printf("%d\n", z);
return 0;
}
a) 0
b) 1
c) Undefined behaviour
d) Compile time error
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
int main()
{
int x = 1;
int y = x == 1 ? getchar(): 2;
printf("%d\n", y);
}
a) Compile time error
b) Whatever character getchar function returns
c) Ascii value of character getchar function returns
d) 2
View Answer
Answer: c
Explanation: None.
Answer: b
Explanation: Space, comma and $ cannot be used in a variable name.
#include <stdio.h>
int main()
{
printf("Hello World! %d \n", x);
return 0;
}
a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
View Answer
Answer: c
Explanation: It results in an error since x is used without declaring the variable x.
Answer: d
Explanation: #define PI 3.14 is a macro preprocessor, it is a textual substitution.
#include <stdio.h>
int main()
{
int main = 3;
printf("%d", main);
return 0;
}
a) It will cause a compile-time error
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping
View Answer
Answer: c
Explanation: A C program can have same function name and same variable name.
Answer: a
Explanation: volatile is C keyword.
Answer: a
Explanation: None.
2. Do logical operators in the C language are evaluated with the short circuit?
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer
Answer: a
Explanation: None.
Answer: b
Explanation: None.
#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf("%d", d);
}
a) Syntax error
b) 1
c) 5
d) 10
View Answer
Answer: b
Explanation: None.
5. Which among the following is NOT a logical or relational operator?
a) !=
b) ==
c) ||
d) =
View Answer
Answer: d
Explanation: None.
Answer: a
Explanation: None.
#include <stdio.h>
void main()
{
float x = 0.1;
if (x == 0.1)
printf("core");
else
printf("Advanced C Classes");
}
a) Advanced C Classes
b) core
c) Run time error
d) Compile time error
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
void main()
{
float x = 0.1;
printf("%d, ", x);
printf("%f", x);
}
a) 0.100000, junk value
b) Junk value, 0.100000
c) 0, 0.100000
d) 0, 0.999999
View Answer
Answer: b
Explanation: None.
#include <stdio.h>
void main()
{
float x;
int y;
printf("enter two numbers \n", x);
scanf("%f %f", &x, &y);
printf("%f, %d", x, y);
}
a) 7.000000, 7
b) Run time error
c) 7.000000, junk
d) Varies
View Answer
Answer: c
Answer: c
Explanation: None.
#include <stdio.h>
int main()
{
int i = 0;
int x = i++, y = ++i;
printf("%d % d\n", x, y);
return 0;
}
a) 0, 2
b) 0, 1
c) 1, 2
d) Undefined
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
int main()
{
int a = 1, b = 1, c;
c = a++ + b;
printf("%d, %d", a, b);
}
a) a = 1, b = 1
b) a = 2, b = 1
c) a = 1, b = 2
d) a = 2, b = 2
View Answer
Answer: b
Explanation: None.
#include <stdio.h>
int main()
{
int a = 1, b = 1, d = 1;
printf("%d, %d, %d", ++a + ++a+a++, a++ + ++b, ++d + d++ + a++);
}
a) 15, 4, 5
b) 9, 6, 9
c) 9, 3, 5
d) Undefined (Compiler Dependent)
View Answer
Answer: d
Explanation: None.
4. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int c = 2 ^ 3;
printf("%d\n", c);
}
a) 1
b) 8
c) 9
d) 0
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
int main()
{
reverse(1);
}
void reverse(int i)
{
if (i > 5)
exit(0);
printf("%d\n", i);
return reverse(i++);
}
a) 1 2 3 4 5
b) 1 2 3 4
c) Compile time error
d) Stack overflow
View Answer
Answer: d
#include <stdio.h>
int main()
{
int x = 2, y = 0;
int z;
z = (y++, y);
printf("%d\n", z);
return 0;
}
a) 0
b) 1
c) Undefined behaviour
d) Compilation error
View Answer
Answer: b
Explanation: None.
#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
View Answer
Answer: a
#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
View Answer
Answer: b
1. What will be the output of the following C code? (Assuming that we have entered the value 1 in the
standard input)
#include <stdio.h>
void main()
{
double ch;
printf("enter a value between 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
View Answer
Answer: a
Explanation: None.
Answer: a
Explanation: None.
2. 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–)
View Answer
Answer: d
Explanation: None.
#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
View Answer
Answer: c
Explanation: None.
#include <stdio.h>
int main()
{
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
View Answer
Answer: b
Answer: b
Explanation: None.
#include <stdio.h>
int main()
{
int a = 0, i = 0, b;
for (i = 0;i < 5; i++)
{
a++;
continue;
}
}
a) 2
b) 3
c) 4
d) 5
View Answer
Answer: d
Explanation: None.
#include <stdio.h>
int main()
{
int a = 0, i = 0, b;
for (i = 0;i < 5; i++)
{
a++;
if (i == 3)
break;
}
}
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: d
Explanation: None.
Answer: b
Explanation: None.
5. Which keyword is used to come out of a loop only for that iteration?
a) break
b) continue
c) return
d) none of the mentioned
View Answer
Answer: b
Explanation: None.
#include <stdio.h>
int main()
{
printf("%d ", 1);
goto l1;
printf("%d ", 2);
l1:goto l2;
printf("%d ", 3);
l2:printf("%d ", 4);
}
a) 1 4
b) Compilation error
c) 1 2 4
d) 1 3 4
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
int main()
{
printf("%d ", 1);
l1:l2:
printf("%d ", 2);
printf("%d\n", 3);
}
a) Compilation error
b) 1 2 3
c) 1 2
d) 1 3
View Answer
Answer: b
#include <stdio.h>
int main()
{
void foo();
printf("1 ");
foo();
}
void foo()
{
printf("2 ");
}
a) 1 2
b) Compile time error
c) 1 2 1 2
d) Depends on the compiler
View Answer
Answer: a
Explanation: None.
C Programming Questions and Answers – Functions Returning Non-integers – 1
Answer: c
Explanation: None.
2. What will be the data type returned for the following C function?
#include <stdio.h>
int func()
{
return (double)(char)5.0;
}
a) char
b) int
c) double
d) multiple type-casting in return is illegal
View Answer
Answer: b
Explanation: None.
#include <stdio.h>
void main()
{
m();
printf("%d", x);
}
int x;
void m()
{
x = 4;
}
a) 4
b) Compile time error
c) 0
d) Undefined
View Answer
Answer: b
Explanation: None.
#include <stdio.h>
int i;
int main()
{
extern int i;
if (i == 0)
printf("scope rules\n");
}
a) scope rules
b) Compile time error due to multiple declaration
c) Compile time error due to not defining type in statement extern i
d) Nothing will be printed as value of i is not zero because i is an automatic variable
View Answer
Answer: a
Explanation: None.
2. What will be the output of the following C code (without linking the source file in which ary1 is
defined)?
#include <stdio.h>
int main()
{
extern ary1[];
printf("scope rules\n");
}
a) scope rules
b) Linking error due to undefined reference
c) Compile time error because size of array is not provided
d) Compile time error because datatype of array is not provided
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
int b;
int main()
{
int c;
return 0;
}
int a;
a) a
b) b
c) c
d) Both a and b
View Answer
Answer: b
Explanation: None.
#include <stdio.h>
void main()
{
m();
m();
}
void m()
{
static int x = 5;
x++;
printf("%d", x);
}
a) 6 7
b) 6 6
c) 5 5
d) 5 6
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
int main()
{
register int i = 10;
int *p = &i;
*p = 11;
printf("%d %d\n", i, *p);
}
a) Depends on whether i is actually stored in machine register
b) 10 10
c) 11 11
d) Compile time error
View Answer
Answer: d
Explanation: None.
Answer: b
Explanation: None.
Answer: d
Explanation: None.
Answer: a
Explanation: None.
Answer: a
Explanation: None.
#include <stdio.h>
int main()
{
int i = 10;
void *p = &i;
printf("%d\n", (int)*p);
return 0;
}
a) Compile time error
b) Segmentation fault/runtime crash
c) 10
d) Undefined behaviour
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
void main()
{
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
View Answer
Answer: a
Explanation: None.
#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
View Answer
Answer: b
Explanation: None.
#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
View Answer
Answer: b
Explanation: None.
#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
View Answer
Answer: d
Explanation: None.
#include <stdio.h>
int main()
{
char *str = "hello, world\n";
char *strc = "good morning\n";
strcpy(strc, str);
printf("%s\n", strc);
return 0;
}
a) hello, world
b) Crash/segmentation fault
c) Undefined behaviour
d) Run time error
View Answer
Answer: b
Explanation: None.
#include <stdio.h>
int main()
{
char *str = "hello world";
char strc[] = "good morning india\n";
strcpy(strc, str);
printf("%s\n", strc);
return 0;
}
a) hello world
b) hello worldg india
c) Compile time error
d) Undefined behaviour
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **m);
}
a) 5 5 5
b) 5 5 junk value
c) 5 junk junk
d) Run time error
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **p);
}
a) 5 5 5
b) 5 5 junk value
c) 5 junk junk
d) Compile time error
View Answer
Answer: d
Explanation: None.
#include <stdio.h>
void main()
{
int a[2][3] = {1, 2, 3, 4, 5};
int i = 0, j = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
printf("%d", a[i][j]);
}
a) 1 2 3 4 5 0
b) 1 2 3 4 5 junk
c) 1 2 3 4 5 5
d) Run time error
View Answer
Answer: a
Explanation: None.
#include <stdio.h>
void main()
{
int a[2][3] = {1, 2, 3, , 4, 5};
int i = 0, j = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
printf("%d", a[i][j]);
}
a) 1 2 3 junk 4 5
b) Compile time error
c) 1 2 3 0 4 5
d) 1 2 3 3 4 5
View Answer
Answer: b
Answer: b
Explanation: None.
Answer: d
Explanation: None.
Answer: c
Explanation: None.
Answer: b
#include <stdio.h>
struct student
{
int no = 5;
char name[20];
};
void main()
{
struct student s;
s.no = 8;
printf("hello");
}
a) Nothing
b) Compile time error
c) hello
d) Varies
View Answer
Answer: b
Explanation: None.
#include <stdio.h>
struct student
{
char *name;
};
void main()
{
struct student s, m;
s.name = "st";
m = s;
printf("%s%s", s.name, m.name);
}
a) Compile time error
b) Nothing
c) Junk values
d) st st
View Answer
Answer: d
Explanation: None.
Answer: d
Explanation: None.
Answer: d
Explanation: None.
#include <stdio.h>
struct temp
{
int a;
int b;
int c;
};
main()
{
struct temp p[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
}
a) No Compile time error, generates an array of structure of size 3
b) No Compile time error, generates an array of structure of size 9
c) Compile time error, illegal declaration of a multidimensional array
d) Compile time error, illegal assignment to members of structure
View Answer
Answer: a
Explanation: None.
Answer: d
Explanation: None.
3. What will be the output of the following C code? (Assuming size of int be 4)
#include <stdio.h>
struct temp
{
int a;
int b;
int c;
} p[] = {0};
main()
{
printf("%d", sizeof(p));
}
a) 4
b) 12
c) 16
d) Can’t be estimated due to ambiguous initialization of array
View Answer
Answer: b
Answer: c
Explanation: None.
2. Which member of the union will be active after REF LINE in the following C code?
#include <stdio.h>
union temp
{
int a;
float b;
char c;
};
union temp s = {1,2.5,’A’}; //REF LINE
a) a
b) b
c) c
d) Such declaration are illegal
View Answer
Answer: a
Explanation: None.
3. What would be the size of the following union declaration? (Assuming size of double = 8, size of int =
4, size of char = 1)
#include <stdio.h>
union uTemp
{
double a;
int b[10];
char c;
}u;
a) 4
b) 8
c) 40
d) 80
View Answer
Answer: c
Explanation: None.
Answer: c
Explanation: None.