Elance C Test Answers 2015
·
What is the only function all C programs must
contain?
program()
main()
start()
What are the different types of floating-point
data in C ?
short int, double, long int
float, double
double, long int, float
float, double, long double
#ifdef __APPLE__ # include <dir/x.h>
#else # include <other_dir/x.h> #endif What does it mean?
It will define __APPLE__ and include dir/x.h
It will define __APPLE__, include dir/x.h and next
time will include other_dir/x.h
It will include dir/x.h if __APPLE__ is not defined,
or other_dir/x.h, otherwise.
It will include dir/x.h if __APPLE__ is defined, or
other_dir/x.h, otherwise.
In C, a block is defined by...
tabulations
curly braces
tags
angle brackets
indentation
Which of the following special symbols are
allowed in a variable name?
| (pipeline)
- (hyphen)
* (asterisk)
_ (underscore)
int *a, b; What is b ?
An int *
It does not compile
An int
which of these is not valid keyword?
int
double
float
var
char
Which of the following is not a predefined
variable type?
int
real
float
The end of a C statement is indicated by this
character.
+
.
;
:
Which of the following is the correct operator
to compare two integer variables?
==
=
equal
:=
Is C Object Oriented?
Yes
No
int i = 17 / 3; what is the value of i ?
6.0
5.666666
6
5.60
5
In C language, && is a
Arithmetic Operator
Relational Operator
None of them
Logical operator
What is the value of the variable x? int x; x
= 32 / 64;
0
Undefined
0.5
A C variable can start with a digit as well a
letter.
True
False
What is i after the following block of code is
executed : int i; i = 10/5/2/1;
4
5
1
0
int tab[3] = {0,1,2}; int i = 0; tab[++i] == ?
2
0
1
What does "int *p = malloc(2);" do?
It will make p point to the number 2.
It will crash your program (an int is four bytes
long, not two).
Nothing, it will yield a type mismatch compiler
error.
It will make p point to an uninitialized two-byte
piece of memory allocated from the heap.
What will be the output of: #include
<stdio.h> void main() { char a[6] = "Hello";
printf("%d", sizeof(a)); }
Array not initialized correctly
6
Program will not execute.
Compile time error
If we pass an array as an argument of a
function, what exactly get passed?
a[last]th value of array
a[0]th value of array
All elements of an array
Address of array
Which one is NOT a reserved keyword?
static
struct
intern
extern
switch
How can you make an infinite loop in C?
while(1) { }
for(;;) { }
All answers are right.
loop: ... goto loop;
Function Overloading is not supported in C.
True
False
In C ...
Strings are surrounded with double quotes, and
Character with single-quotes.
Strings does not exists in C.
Strings and chars can be surrounded with double
quotes or single-quotes.
What will be the output of this Program?
#include <stdio.h> struct Data{ char a; char *data; int value; }; main()
{ printf("%d\n",sizeof(struct Data)); }
3
It depends on the compiler and the hardware
architecture.
9
6
12
The system function longjmp() can be used to
return execution control to any user-specified point in the active function
call tree.
True
False
What is the value of p in int a,b,*p; p=&a;
b=**p; printf("%d",p);
value of variable a
value of variable b
address of variable a
address of variable b
Will this loop terminate? int x=10; while( x--
> 0 );
no
It will cause segfault
It will not compile
yes
What will be the output if the following
program: #include<stdio.h> int main(){ int a,b; a= -3 - - 25; b= -5 - (-
29); printf("a= %d b=%d", a, b); return 0; }
a=22 b=24
a=28 b=34
a=22 b=34
a=28 b=24
Which statement is true about double?
it uses the GPU
it's an alias of float
its size is 128 bits
its size depends on the implementation
How can you access the first element of an
array called 'arr'?
arr[0]
(both of these)
*arr
char* buf[100]; strcpy(buf, argv[1]); Which
security risk is this code vulnerable to?
Heap overflow
Stack overflow
Integer overflow
Race condition
Format string
What will the following code print? void *p =
malloc(0); printf ("%d\n", p);
Nothing, it will give a runtime error.
Nothing, it won't compile.
0
Unknown, it depends on what malloc will return.
What is the output of printf("%d\n",
sizeof(long) / sizeof(int))?
2
Depends on the implementation, but always some
number > 1.
4
Depends on the implementation, but always some
number >= 1.
1
stdarg.h defines?
formal arguments
macros used with variable argument functions
arguments with data types
actual arguments
array definitions
foo[4] is equivalent of :
There is no equivalent using those notations
*(foo + 4)
(*foo + 4)
&(foo + 4)
*(&foo + 4)
What is the value of 1 & 2?
2
0
1
3
To send an array as a parameter to function,
which one is a right way:
doThis(*array)
doThis(array)
doThis(array[size])
doThis(&array)
What will this code print out? #include
<stdio.h> void function(char *name) { name=NULL; } main() { char *name="ELANCE";
function(name); printf("%s",name); }
NULL
ELANCE
It Won't Compile
Sengmentation Fault
With: sizeof(char *) == 4 sizeof(char) == 1
What will sizeof(plop) for char plop[2][3] be?
14
18
10
6
What is the output of the following code? char
* str1 = "abcd"; char * str2 = "xyz"; if( str1 < str2 )
printf( "1" ); else printf( "2" );
Undefined
2
1
What does malloc(0) return?
The behavior is implementation-defined
A unique pointer
NULL
The program segfault
Predict the output :: main() { float a = 1;
switch(a) { case 1 : printf("Hi"); case 1.0 :
printf("Hello"); default : printf("Default"); break; } }
Default
Hello
HiHelloDefault
Hi
Error, float can't be used in switch
How many "-" will be printed out by
running the following code: #include <stdio.h> #include
<sys/types.h> #include <unistd.h> int main(void) { int i; for(i=0;
i<2; i++){ fork(); printf("-"); } return 0; }
6
2
4
8
What will be the output? main() { float a=1.1;
double b=1.1; if(a==b) printf("True"); else
printf("False"); }
Normally False, but could be True depending on
implementation.
Always True, regardless of implementation.
Error, it's not legal to compare a float with a
double without a cast.
What will be the output of the following
program: #include<stdio.h> int main(){ int a[] = { 1, 2, 4, 8, 16, 32 };
int *ptr, b; ptr = a + 64; b = ptr - a; printf("%d",b); return 0; }
64
Segmentation violation
16
8
32
Which one is not a bitwise operator ?
<<
~
^
|
!
struct mystruct { int a; float b; }; struct
mystruct *ptr; Which of these expressions will access member a?
*ptr->a;
(*ptr).a;
*(ptr).a;
ptr.a;
The main() function can be called recursively.
False
True
What does the following do? int j = 10; while
(j --> 0){ printf("%d ", j); }printf("\n");
9 8 7 6 5 4 3 2 1 0
It segfaults
10 9 8 7 6 5 4 3 2 1
Operator --> does not exist
memmove() is safer than memcpy()
True
False
What is the output of the following code: char
str[10] = "abcd"; char * p1, *p2; p1 = str; p2 = str; *p2 = 'A';
printf ( "%s %s", p1, p2 );
abcd Abcd
Abcd Abcd
Undefined
It won't compile
What is meaning of following declaration?
int(*p[3])();
p is pointer to array of functions
p is array of pointers to functions
p is pointer to function
p is pointer to function that return array
None of these
What is the difference between fopen and open?
open is safer than fopen.
open is deprecated, fopen is not
fopen open files. open can open files and directory.
fopen opens a stream which is buffered.
Which of the following data type is scalar?
Array
Float
Union
Pointer
What will be the output? void main() { int
const *p = 9; printf("%d", ++(*p)); }
Runtime error
10
Compiler Error
9
#include <stdio.h> #define FUNC(A,B)
#B#A int main() { printf(FUNC(AA,BB)); } What does it print?
BB
AABB
BBAA
Nothing! It doesn't compile
AA
What is the output of the following code? char
* str1 = "abcd"; char * str2 = "xyz"; printf ( "%d
%d", strlen(str1) - strlen(str2), sizeof(str1) - sizeof(str2));
1 undefined
1 0
Compile time error
1 1
Sort these integer constants from smallest to
largest.
0x40, 40, 040
040, 40, 0x40
0x40, 040, 40
40, 040, 0x40
What is the difference between "void
foo();" and "void foo(void);" in C?
void foo() allows for calls with any number of
arguments while void foo(void) does not allow for calls with any arguments.
void foo(); is not correct C.
Neither void foo(); nor void foo(void); are correct
C.
void foo(void); is not correct C.
There is no difference, they are equivalent.
What will be the output? #include
<stdio.h> int main() { char c = 125; c = c + 10; printf("%d",
c); return 0; }
7
It is implementation dependent.
8
-121
135
int a = 1, b; a = 1 & b = 2; what's the
result of a and b?
1, 2
It doesn't compile.
1, undefined
0, 2
undefined, undefined
Which one is a reserved keyword?
real
redirect
restrict
reserved
rearrange
What is the output of the following program:
#include "stdio.h" int main( ) { printf( "%d %d",
sizeof(" "),sizeof(NULL) ); return 0; }
Nothing appear, program closed after execution.
Compile time error
None of these
NULL
What is the output of the Program : main() {
int i,j; i=1,2,3; j=(1,2,3); printf("%d %d",i,j); }
Compile Time Error
1 1
1 3
0 0
Garbage Values of i & j
What are i and j after this code is executed??
#define Product(x) (x*x) main() { int i , j; i = Product(2); j = Product(1 +
1); printf("%d %d\n", i , j); }
4 3
4 1
2 2
4 4
Error in Statement :: Product ( 1 + 1 )
#include <stdio.h> int main() { int n; n
= 2, 4, 6; printf("%d", n); return 0; } what is the output?
compiler error
4
2
6
undefined
Is this code valid in C89? #define RED
"\033[31m" #define END "\033[0m" puts("Hello "
RED "world" END " !");
It's a GNU extension
Yes
No
For a 3 dimensions array[2][3][4] how many
call to malloc do you need at least?
1
3
4
24
2
What is the output: register int n;
printf("%u",&n);
garbage value
address of n
run time error
runs with warning
compile time error
What does this code do? printf("%u",
-08);
Prints whatever number UINT_MAX-7 is.
Prints the number -8.
Prints whatever number UINT_MAX-8 is.
It doesn't even compile.
Currently, what does GCC stand for?
Great C Compiler
GNU C Compiler
GNU Compiler Collection
GNU Code Compiler
with sizeof(char) == 1 sizeof(char *) == 4
char *a, *b; a = b; b++; What is (b - a)?
1
It depends
4
It segfaults
#include<stdio.h> int main(){ char
*string="smarterer";
printf("%d",printf("%s",string)); return 0; } What is the
output?
garbage value
smarterer10
smarterer9
Compiler error
9smarterer
This code will: void main() { char const *p =
"abcdef"; printf("%s",*(p+4)); }
not compile
print "def"
print "e"
print "ef"
cause an error when run
#include <stdio.h> #define SQR(x) (x*x)
void main(void) { int x, y; x = 5; y = SQR(++x); printf("y is %d\n",
y); }
The result is indeterminate. It depends on how the
compiler sequences the two pre-increment operations.
y is 49
y is 36
y is 25
y is 30
What is the value of i after : int i; i = 10;
i = i++ + i++;
22
It's implementation dependent
23
21
20
Give the output :: main() { int i=10 , j=0,
a,b; a = i || j++ ; b = j && i++; printf("%d, %d, %d,
%d",a,b,i,j); }
1, 1, 10, 0
1, 0, 11 ,1
1, 1, 11, 1
1, 0, 10, 0
0, 1, 10, 0
What is the output of the following program:
#include <stdio.h> void main() { int j,k=10,l=-20,a=8,b=4; j = --k - ++l
* b / a; printf("Z= %d \n",j); }
Compile time error
Z=20
Z=18
Z=16
Which command-line flag will dump the output
of the preprocessor from gcc?
-H
--preprocessing
-C
-E
Which of these is NOT correct syntax?
struct stru { int a[10]; int b=0; };
struct struct { int a; float b; };
struct { int a; float b; };
#include <stdio.h> int main() { int
i=-1, j=-1; (i = 0) && (j = 0); (i++) && (++j);
printf("%d,%d\n", i, j); return 0; } What is the output of that program?
1, 1
0, 0
1, 0
1, -1
0, 1
int a=2, b=3, c=4; int res; res=c>b>a;
What is the value of res?
-1
0
Compilation error
1
We don't know
What will be the output of this code? #include
<stdio.h> int main(void) { int i = 1; printf("%d", i+++i);
return 0; }
4
2
Compiler error
2 or 3 depending on evaluation order
3
After char *foo = calloc(1, 2); what is the
value of *foo?
0
Unknown, it depends on what memory block it got
assigned to.
2
1
Does "gcc -Wall" print all warnings?
No
Yes
It depends
What does "11 | 4 == 15" evaluate
to?
1
15
0
11
What will the following code print? void *p =
malloc(0); printf ("%d\n", *p);
Unknown, it depends on what pointer malloc returns.
Nothing, it will give a runtime error.
0
Nothing, it won't compile.
Subscribe to:
Posts (Atom)