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 if it is executed like below?
cmd> sample

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

int main(int argc, char **argv)
{
    printf("%s\n", argv[argc-1]);
    return 0;
}

A.
0
B.
sample
C.
samp
D.
No output

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum

2.

What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three

/* myprog.c */
#include<stdio.h>

int main(int argc, char **argv)
{
    printf("%c\n", **++argv);
    return 0;
}

A.
myprog one two three
B.
myprog one
C.
o
D.
two

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum

3.

What will be the output of the program?

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

A.
3
B.
4
C.
5
D.
6

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum

4.

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

5.

Which statement will you add to the following program to ensure that the program outputs "IndiaBIX" on execution?

#include<stdio.h>

int main()
{
    char s[] = "IndiaBIX";
    char t[25];
    char *ps, *pt;
    ps = s;
    pt = t;
    while(*ps)
        *pt++ = *ps++;

    /* Add a statement here */
    printf("%s\n", t);
    return 0;
}

A.
*pt='';
B.
pt='\0';
C.
pt='\n';
D.
*pt='\0';

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum

6.

Are the expressions arr and &arr same for an array of 10 integers?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Both mean two different things. arr gives the address of the first int, whereas the &arr gives the address of array of ints.

Learn more problems on : Arrays

Discuss about this problem : Discuss in Forum

7.

We want to test whether a value lies in the range 2 to 4 or 5 to 7. Can we do this using a switch?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

We can do this in following switch statement

switch(a)
{
    case 2:
    case 3:
    case 4:
       /* some statements */
       break;
    case 5:
    case 6:
    case 7:
       /* some statements */
       break;
}

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum

8.

The keyword used to transfer control from a function back to the calling function is

A.
switch
B.
goto
C.
go back
D.
return

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The keyword return is used to transfer control from a function back to the calling function.

Example:


#include<stdio.h>
int add(int, int); /* Function prototype */

int main()
{
    int a = 4, b = 3, c;
    c = add(a, b);
    printf("c = %d\n", c);
    return 0;
}
int add(int a, int b)
{
/* returns the value and control back to main() function */
   return (a+b);
}

Output:
c = 7

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum

9.

What will be the output of the program?

#include<stdio.h>
#include<stdarg.h>
void dumplist(int, ...);

int main()
{
    dumplist(2, 4, 8);
    dumplist(3, 6, 9, 7);
    return 0;
}
void dumplist(int n, ...)
{
    va_list p; int i;
    va_start(p, n);

    while(n-->0)
    {
        i = va_arg(p, int);
        printf("%d", i);
    }
    va_end(p);
    printf("\n");
}

A.
2 4
3 6
B.
2 4 8
3, 6, 9, 7
C.
4 8
6 9 7
D.
1 1 1
1 1 1 1

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum

10.

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

11.

Point out the error in the program in 16-bit platform?

#include<stdio.h>

int main()
{
    struct bits
    {
        int i:40;
    }bit;

    printf("%d\n", sizeof(bit));
    return 0;
}

A.
4
B.
2
C.
Error: Bit field too large
D.
Error: Invalid member access in structure

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum

12.

Input/output function prototypes and macros are defined in which header file?

A.
conio.h
B.
stdlib.h
C.
stdio.h
D.
dos.h

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations.

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum

13.

Which of the following is the correct order if calling functions in the below code?
a = f1(23, 14) * f2(12/4) + f3();

A.
f1, f2, f3
B.
f3, f2, f1
C.
Order may vary from compiler to compiler
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Here, Multiplication will happen before the addition, but in which order the functions would be called is undefined. In an arithmetic expression the parenthesis tell the compiler which operands go with which operators but do not force the compiler to evaluate everything within the parenthesis first.

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum

14.

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

15.

Which of the following sentences are correct about a for loop in a C program?
1: for loop works faster than a while loop.
2: All things that can be done using a for loop can also be done using a while loop.
3: for(;;); implements an infinite loop.
4: for loop can be used if we want statements in a loop get executed at least once.

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum

16.

What will be the output of the program?

#include<stdio.h>
typedef unsigned long int uli;
typedef uli u;

int main()
{
    uli a;
    u b = -1;
    a = -1;
    printf("%lu, %lu", a, b);
    return 0;
}

A.
4343445454, 4343445454
B.
4545455434, 4545455434
C.
4294967295, 4294967295
D.
Garbage values

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The system will treat the negative numbers with 2's complement method.

For 'long int' system will occupy 4 bytes (32 bits).

Therefore,

Binary 1 : 00000000 00000000 00000000 00000001

To represent -1, system uses the 2's complement value of 1. Add 1 to the 1's complement result to obtain 2's complement of 1.

So, First take 1's complement of binary 1 (change all 0s to 1s and all 1s to 0s)

1's complement of Binary 1:

11111111 11111111 11111111 11111110

2's complement of Binary 1: (Add 1 with the above result)

11111111 11111111 11111111 11111111


In HexaDecimal

11111111 11111111 11111111 11111111 = FFFF FFFF FFFF FFFF

In Unsigned Integer

11111111 11111111 11111111 11111111 = 4294967295.

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum

17.

What will be the output of the program?

#include<stdio.h>
#define MAX(a, b) (a > b ? a : b)

int main()
{
    int x;
    x = MAX(3+2, 2+7);
    printf("%d\n", x);
    return 0;
}

A.
8
B.
9
C.
6
D.
5

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The macro MAX(a, b) (a > b ? a : b) returns the biggest value of the given two numbers.

Step 1 : int x; The variable x is declared as an integer type.

Step 2 : x = MAX(3+2, 2+7); becomes,

=> x = (3+2 > 2+7 ? 3+2 : 2+7)

=> x = (5 > 9 ? 5 : 9)

=> x = 9

Step 3 : printf("%d\n", x); It prints the value of variable x.

Hence the output of the program is 9.

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.

Functions cannot return a floating point number

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

A function can return floating point value.

Example:


#include <stdio.h>
float sub(float, float); /* Function prototype */

int main()
{
    float a = 4.5, b = 3.2, c;
    c = sub(a, b);
    printf("c = %f\n", c);
    return 0;
}
float sub(float a, float b)
{
   return (a - b);
}

Output:
c = 1.300000

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum

20.

What will be the output of the program?

#include<stdio.h>

int main()
{
    int y=128;
    const int x=y;
    printf("%d\n", x);
    return 0;
}

A.
128
B.
Garbage value
C.
Error
D.
0

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Step 1: int y=128; The variable 'y' is declared as an integer type and initialized to value "128".

Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with the variable 'y' value.

Step 3: printf("%d\n", x); It prints the value of variable 'x'.

Hence the output of the program is "128"

Learn more problems on : Const

Discuss about this problem : Discuss in Forum

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