A Learning Portal from Recruitment India
What will be output if you will compile and execute the following c code?
struct marks{
int p:3;
int c:3;
int m:2;
};
void main(){
struct marks s={2,-6,5};
printf(“%d %d %d”,s.p,s.c,s.m);
}
2 -6 5
2 -6 1
2 2 1
Compiler error
Answer with explanation
Answer: Option CExplanation
Binary value of 2: 00000010 (Select three two bit)
Binary value of 6: 00000110
Binary value of -6: 11111001+1=11111010
(Select last three bit)
Binary value of 5: 00000101 (Select last two bit)
Workspace
The preprocessor provides the ability for _______________.
The inclusion of header files
The inclusion of macro expansions
Conditional compilation and line control
All of the mentioned
Answer with explanation
Answer: Option DExplanation
The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control.
Workspace
#include<stdio.h>
#define a 10
void main()
{
#define a 50
printf("%d", a);
}
50
10
Compiler Error
None of these
Answer with explanation
Answer: Option AExplanation
The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken.
Workspace
3
4
no output
Answer with explanation
Answer: Option AExplanation
Workspace
What will be output if you will compile and execute the following c code?
void main(){
static main;
int x;
x=call(main);
clrscr();
printf(“%d “,x);
getch();
}
int call(int address){
address++;
return address;
}
1
Garbage value
Compiler error
Answer with explanation
Answer: Option BExplanation
1
Workspace
#pragma exit is primarily used for?
Checking memory leaks after exiting the program
Informing Operating System that program has terminated
Running a function at exiting the program
No such preprocessor exist
Answer with explanation
Answer: Option CExplanation
It is primarily used for running a function upon exitting the program.
Workspace
ink
ite
let
ack
Answer with explanation
Answer: Option AExplanation
ink
Workspace
A “C” variable cannot start with
A number
A special symbol other than underscore
Both of the above
An alphabet
Answer with explanation
Answer: Option CExplanation
A “C” variable cannot start with A special symbol other than underscore and A number
Workspace
What is the correct value to return to the operating system upon the successful completion of a program?
1
-12
2
Answer with explanation
Answer: Option AExplanation
the correct value to return to the operating system upon the successful completion of a program is “0”
Workspace
printf(“My salary was increased by 15/%!”);
printf(“My salary was increased by 15%!”);
printf(“My salary was increased by 15’%’!”);
printf(“My salary was increased by 15%%!”);
Answer with explanation
Answer: Option DExplanation
printf(“My salary was increased by 15%%!”);
Workspace
What will be the output of the following C code?
#include <stdio.h>
void main()
{
int x = 1, z = 3;
int y = x << 3;
printf(" %d\n", y);
}
-2147483648
-1
Run time error
8
Answer with explanation
Answer: Option DExplanation
as we know binary code for 1 is 0001
so it states x<<3 means we have to shift value 3 times to right so
0001→ 0010→ 0100→ 1000
1000 = 8
Workspace
What will be the output of the following C code?
#include <stdio.h>
void main()
{
int y = 3;
int x = 5 % 2 * 3 / 2;
printf("Value of x is %d", x);
}
Value of x is 1
Value of x is 2
Value of x is 3
Compile time error
Answer with explanation
Answer: Option AExplanation
Value of x is 1
Workspace
The name of the variable used in one function cannot be used in another function.
True
False
May be
None of the mentioned
Answer with explanation
Answer: Option BExplanation
Since the scope of the variable declared within a function is restricted only within that function, the same name can be used to declare another variable in another function.
Workspace
What will be the output of the following C code?
#include <stdio.h>
int main()
{
j = 10;
printf("%d\n", j++);
return 0;
}
10
11
Compile time error
Answer with explanation
Answer: Option CExplanation
Variable j is not defined.
Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:4: error: ‘j’ undeclared (first use in this function)
pgm3.c:4: error: (Each undeclared identifier is reported only once
pgm3.c:4: error: for each function it appears in.)
Workspace
What will be the output of the following C code?
#include <stdio.h>
int main()
{
float f1 = 0.1;
if (f1 == 0.1f)
printf("equal\n");
else
printf("not equal\n");
}
equal
not equal
output depends on compiler
none of the mentioned
Answer with explanation
Answer: Option AExplanation
0.1f results in 0.1 to be stored in floating point representations.
Output:
$ cc pgm5.c
$ a.out
equal
Workspace
What will be the output of the following C code on a 32-bit machine?
#include <stdio.h>
int main()
{
int x = 10000;
double y = 56;
int *p = &x;
double *q = &y;
printf("p and q are %d and %d", sizeof(p), sizeof(q));
return 0;
}
p and q are 4 and 4
p and q are 4 and 8
compiler error
p and q are 2 and 8
Answer with explanation
Answer: Option AExplanation
Size of any type of pointer is 4 on a 32-bit machine.
Output:
$ cc pgm6.c
$ a.out
p and q are 4 and 4
Workspace
What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 3;
int l = i / -2;
int k = i % -2;
printf("%d %d\n", l, k);
return 0;
}
Compile time error
-1 1
1 -1
Implementation defined
Answer with explanation
Answer: Option BExplanation
-1 1
Workspace
What will be the output of the following C code?
#include <stdio.h>
void main()
{
int k = 4;
float k = 4;
printf("%d", k)
}
Compile time error
4
4.0000000
4.4
Answer with explanation
Answer: Option AExplanation
Since the variable k is defined both as integer and as float, it results in an error.
Output:
$ cc pgm8.c
pgm8.c: In function ‘main’:
pgm8.c:5: error: conflicting types for ‘k’
pgm8.c:4: note: previous definition of ‘k’ was here
pgm8.c:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
pgm8.c:7: error: expected ‘;’ before ‘}’ token
Workspace
What will be the output of the following C code?
#include <stdio.h>
int main()
{
const int i = 10;
int *ptr = &i;
*ptr = 20;
printf("%d\n", i);
return 0;
}
Compile time error
Compile time warning and printf displays 20
Undefined behaviour
10
Answer with explanation
Answer: Option BExplanation
Changing const variable through non-constant pointers invokes compiler warning.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: warning: initialization discards qualifiers from pointer target type
$ a.out
20
Workspace
What will be the output of the following C code?
#include <stdio.h>
void main()
{
int const k = 5;
k++;
printf("k is %d", k);
}
k is 6
Error due to const succeeding int
Error, because a constant variable can be changed only twice
Error, because a constant variable cannot be changed
Answer with explanation
Answer: Option DExplanation
Constant variable has to be declared and defined at the same time. Trying to change it results in an error.
Output:
$ cc pgm12.c
pgm12.c: In function ‘main’:
pgm12.c:5: error: increment of read-only variable ‘k’
Workspace
What will be the output of the following C code?
#include <stdio.h>
#include <string.h>
int main()
{
char *str = "x";
char c = 'x';
char ary[1];
ary[0] = c;
printf("%d %d", strlen(str), strlen(ary));
return 0;
}
1 1
2 1
2 2
1 (undefined value)
Answer with explanation
Answer: Option DExplanation
str is null terminated, but ary is not null terminated.
Output:
$ cc pgm7.c
$ a.out
1 5
Workspace