Online C Programming Test - C Programming Test - Random



Loading...  Loading Test...

Instruction:

  • Total number of questions : 20.
  • Time alloted : 30 minutes.
  • Each question carry 1 mark, no negative marks.
  • DO NOT refresh the page.
  • All the best :-).

1.

What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample Jan Feb Mar

/* sample.c */
#include<stdio.h>
#include<dos.h>

int main(int arc, char *arv[])
{
    int i;
    for(i=1; i<_argc; i++)
        printf("%s ", _argv[i]);
    return 0;
}

A.
No output
B.
sample Jan Feb Mar
C.
Jan Feb Mar
D.
Error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum

2.

What will be the output of the program in Turbo C?

#include<stdio.h>

int main()
{
    char near *near *ptr1;
    char near *far *ptr2;
    char near *huge *ptr3;
    printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
    return 0;
}

A.
4, 4, 8
B.
4, 4, 4
C.
2, 4, 8
D.
2, 4, 4

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum

3.

Which of the following statements are correct about the program?

#include<stdio.h>
char *fun(unsigned int num, int base);

int main()
{
    char *s;
    s=fun(128, 2);
    s=fun(128, 16);
    printf("%s\n",s);
    return 0;
}
char *fun(unsigned int num, int base)
{
    static char buff[33];
    char *ptr = &buff[sizeof(buff)-1];
    *ptr = '\0';
    do
    {
        *--ptr = "0123456789abcdef"[num %base];
        num /=base;
    }while(num!=0);
    return ptr;
}

A.
It converts a number to a given base.
B.
It converts a number to its equivalent binary.
C.
It converts a number to its equivalent hexadecimal.
D.
It converts a number to its equivalent octal.

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Bitwise Operators

Discuss about this problem : Discuss in Forum

4.

If the two strings are found to be unequal then strcmp returns difference between the first non-matching pair of characters.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

g = strcmp(s1, s2); returns 0 when the strings are equal, a negative integer when s1 is less than s2, or a positive integer if s1 is greater than s2, that strcmp() not only returns -1, 0 and +1, but also other negative or positive values(returns difference between the first non-matching pair of characters between s1 and s2).

A possible implementation for strcmp() in "The Standard C Library".


int strcmp (const char * s1, const char * s2)
{                
	for(; *s1 == *s2; ++s1, ++s2) 
	{
		if(*s1 == 0)
			return 0;
	}
	return *(unsigned char *)s1 < *(unsigned char *)s2 ? -1 : 1;
}

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum

5.

Will it result in to an error if a header file is included twice?

A.
Yes
B.
No
C.
It is compiler dependent.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Unless the header file has taken care to ensure that if already included it doesn't get included again.

Turbo C, GCC compilers would take care of these problems, generate no error.

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum

6.

Will the program compile successfully?

#include<stdio.h>
#define X (4+Y)
#define Y (X+3)

int main()
{
    printf("%d\n", 4*X+2);
    return 0;
}

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Reports an error: Undefined symbol 'X'

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum

7.

To print out a and b given below, which of the following printf() statement will you use?

#include<stdio.h>

float a=3.14;
double b=3.14;

A.
printf("%f %lf", a, b);
B.
printf("%Lf %f", a, b);
C.
printf("%Lf %Lf", a, b);
D.
printf("%f %Lf", a, b);

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

To print a float value, %f is used as format specifier.

To print a double value, %lf is used as format specifier.

Therefore, the answer is printf("%f %lf", a, b);

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum

8.

one of elements of a structure can be a pointer to the same structure.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum

9.

Which of the following statements are correct about the below declarations?
char *p = "Sanjay";
char a[] = "Sanjay";
1: There is no difference in the declarations and both serve the same purpose.
2: p is a non-const pointer pointing to a non-const string, whereas a is a const pointer pointing to a non-const pointer.
3: The pointer p can be modified to point to another string, whereas the individual characters within array a can be changed.
4: In both cases the '\0' will be added at the end of the string "Sanjay".

A.
1, 2
B.
2, 3, 4
C.
3, 4
D.
2, 3

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum

10.

Can you combine the following two statements into one?

char *p;
p = (char*) malloc(100);

A.
char p = *malloc(100);
B.
char *p = (char) malloc(100);
C.
char *p = (char*)malloc(100);
D.
char *p = (char *)(malloc*)(100);

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum

11.

Functions can be called either by value or reference

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

True, A function can be called either call by value or call by reference.

Example:

Call by value means c = sub(a, b); here value of a and b are passed.

Call by reference means c = sub(&a, &b); here address of a and b are passed.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum

12.

A function may have any number of return statements each returning different values.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

True, A function may have any number of return statements each returning different values and each return statements will not occur successively.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum

13.

Bitwise | can be used to set multiple bits in number.

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Bitwise Operators

Discuss about this problem : Discuss in Forum

14.

Which header file should you include, if you are going to develop a function, which can accept variable number of arguments?

A.
varagrg.h
B.
stdlib.h
C.
stdio.h
D.
stdarg.h

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum

15.

The macro va_start is used to initialise a pointer to the beginning of the list of fixed arguments.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum

16.

The macro va_arg is used to extract an argument from the variable argument list and advance the pointer to the next argument.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum

17.

What will be the output of the program?

#include<stdio.h>
#define SQUARE(x) x*x

int main()
{
    float s=10, u=30, t=2, a;
    a = 2*(s-u*t)/SQUARE(t);
    printf("Result = %f", a);
    return 0;
}

A.
Result = -100.000000
B.
Result = -25.000000
C.
Result = 0.000000
D.
Result = 100.000000

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The macro function SQUARE(x) x*x calculate the square of the given number 'x'. (Eg: 102)

Step 1: float s=10, u=30, t=2, a; Here the variable s, u, t, a are declared as an floating point type and the variable s, u, t are initialized to 10, 30, 2.

Step 2: a = 2*(s-u*t)/SQUARE(t); becomes,

=> a = 2 * (10 - 30 * 2) / t * t; Here SQUARE(t) is replaced by macro to t*t .

=> a = 2 * (10 - 30 * 2) / 2 * 2;

=> a = 2 * (10 - 60) / 2 * 2;

=> a = 2 * (-50) / 2 * 2 ;

=> a = 2 * (-25) * 2 ;

=> a = (-50) * 2 ;

=> a = -100;

Step 3: printf("Result=%f", a); It prints the value of variable 'a'.

Hence the output of the program is -100

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum

18.

What will be the output of the program?

#include<stdio.h>
#define MAN(x, y) ((x)>(y)) ? (x):(y);

int main()
{
    int i=10, j=5, k=0;
    k = MAN(++i, j++);
    printf("%d, %d, %d\n", i, j, k);
    return 0;
}

A.
12, 6, 12
B.
11, 5, 11
C.
11, 5, Garbage
D.
12, 6, Garbage

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The macro MAN(x, y) ((x)>(y)) ? (x):(y); returns the biggest number of given two numbers.

Step 1: int i=10, j=5, k=0; The variable i, j, k are declared as an integer type and initialized to value 10, 5, 0 respectively.

Step 2: k = MAN(++i, j++); becomes,

=> k = ((++i)>(j++)) ? (++i):(j++);

=> k = ((11)>(5)) ? (12):(6);

=> k = 12

Step 3: printf("%d, %d, %d\n", i, j, k); It prints the variable i, j, k.

In the above macro step 2 the variable i value is increemented by 2 and variable j value is increemented by 1.

Hence the output of the program is 12, 6, 12

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum

19.

Point out the error in the following program.

#include<stdio.h>
#include<stdarg.h>

int main()
{
    void display(int num, ...);
    display(4, 12.5, 13.5, 14.5, 44.3);
    return 0;
}
void display(int num, ...)
{
    float c; int j;
    va_list ptr;
    va_start(ptr, num);
    for(j=1; j<=num; j++)
    {
        c = va_arg(ptr, float);
        printf("%f", c);
    }
}

A.
Error: invalid va_list declaration
B.
Error: var c data type mismatch
C.
No error
D.
No error and Nothing will print

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Use double instead of float in float c;

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum

20.

What will be the output of the program if the size of pointer is 4-bytes?

#include<stdio.h>

int main()
{
    printf("%d, %d\n", sizeof(NULL), sizeof(""));
    return 0;
}

A.
2, 1
B.
2, 2
C.
4, 1
D.
4, 2

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

In TurboC, the output will be 2, 1 because the size of the pointer is 2 bytes in 16-bit platform.

But in Linux, the output will be 4, 1 because the size of the pointer is 4 bytes.

This difference is due to the platform dependency of C compiler.

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum

Submit your test now to view the Results and Statistics with answer explanation.


Have a question?

Ask your question now !