Online C Programming Test - C Programming Test - Random



Loading...  Loading Test...

Instruction:

  • This is a FREE online test. DO NOT pay money to anyone to attend this test.
  • 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 ?

#include<stdio.h>

int main()
{
    int i;
    char a[] = "\0";
    if(printf("%s", a))
        printf("The string is not empty\n");
    else
        printf("The string is empty\n");
    return 0;
}

A.
The string is not empty
B.
The string is empty
C.
No output
D.
0

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The function printf() returns the number of charecters printed on the console.

Step 1: char a[] = '\0'; The variable a is declared as an array of characters and it initialized with "\0". It denotes that the string is empty.

Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero). Hence the if condition is failed.

In the else part it prints "The string is empty".

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


2.

What will be the output of the program?

#include<stdio.h>

int main()
{
    int i;
    char c;
    for(i=1; i<=5; i++)
    {
        scanf("%c", &c); /* given input is 'b' */
        ungetc(c, stdout);
        printf("%c", c);
        ungetc(c, stdin);
    }
    return 0;
}

A.
bbbb
B.
bbbbb
C.
b
D.
Error in ungetc statement.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The ungetc() function pushes the character c back onto the named input stream, which must be open for reading.

This character will be returned on the next call to getc or fread for that stream.

One character can be pushed back in all situations.

A second call to ungetc without a call to getc will force the previous character to be forgotten.

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


3.

What will be the output of the program?

#include<stdio.h>
int main()
{
    int k, num=30;
    k = (num>5 ? (num <=10 ? 100 : 200): 500);
    printf("%d\n", num);
    return 0;
}

A.
200
B.
30
C.
100
D.
500

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Step 1: int k, num=30; here variable k and num are declared as an integer type and variable num is initialized to '30'.
Step 2: k = (num>5 ? (num <=10 ? 100 : 200): 500); This statement does not affect the output of the program. Because we are going to print the variable num in the next statement. So, we skip this statement.
Step 3: printf("%d\n", num); It prints the value of variable num '30'
Step 3: Hence the output of the program is '30'

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum


4.

Can we use a switch statement to switch on strings?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The cases in a switch must either have integer constants or constant expressions.

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


5.

What will be the output of the program?

#include<stdio.h>
int fun(int **ptr);

int main()
{
    int i=10;
    const int *ptr = &i;
    fun(&ptr);
    return 0;
}
int fun(int **ptr)
{
    int j = 223;
    int *temp = &j;
    printf("Before changing ptr = %5x\n", *ptr);
    const *ptr = temp;
    printf("After changing ptr = %5x\n", *ptr);
    return 0;
}

A.
Address of i
Address of j
B.
10
223
C.
Error: cannot convert parameter 1 from 'const int **' to 'int **'
D.
Garbage value

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Const

Discuss about this problem : Discuss in Forum


6.

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


7.

What will be the output of the program?

#include<stdio.h>
int main()
{
    int a=0, b=1, c=3;
    *((a) ? &b : &a) = a ? b : c;
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Step 1: int a=0, b=1, c=3; here variable a, b, and c are declared as integer type and initialized to 0, 1, 3 respectively.

Step 2: *((a) ? &b : &a) = a ? b : c; The right side of the expression(a?b:c) becomes (0?1:3). Hence it return the value '3'.

The left side of the expression *((a) ? &b : &a) becomes *((0) ? &b : &a). Hence this contains the address of the variable a *(&a).

Step 3: *((a) ? &b : &a) = a ? b : c; Finally this statement becomes *(&a)=3. Hence the variable a has the value '3'.

Step 4: printf("%d, %d, %d\n", a, b, c); It prints "3, 1, 3".

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


8.

What will be the output of the program, if a short int is 2 bytes wide?

#include<stdio.h>
int main()
{
    short int i = 0;
    for(i<=5 && i>=-1; ++i; i>0)
        printf("%u,", i);
    return 0;
}

A.
1 ... 65535
B.
Expression syntax error
C.
No output
D.
0, 1, 2, 3, 4, 5

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

for(i<=5 && i>=-1; ++i; i>0) so expression i<=5 && i>=-1 initializes for loop. expression ++i is the loop condition. expression i>0 is the increment expression.

In for( i <= 5 && i >= -1; ++i; i>0) expression i<=5 && i>=-1 evaluates to one.

Loop condition always get evaluated to true. Also at this point it increases i by one.

An increment_expression i>0 has no effect on value of i.so for loop get executed till the limit of integer (ie. 65535)

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


9.

What will be the output of the program?

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

A.
4, 3, 2, 1, 0, -1
4, 3, 2, 1, 0, -1
B.
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
C.
Error
D.
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Step 1: Initially the value of variable i is '5'.
Loop 1: while(i-- >= 0) here i = 5, this statement becomes while(5-- >= 0) Hence the while condition is satisfied and it prints '4'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 2: while(i-- >= 0) here i = 4, this statement becomes while(4-- >= 0) Hence the while condition is satisfied and it prints '3'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 3: while(i-- >= 0) here i = 3, this statement becomes while(3-- >= 0) Hence the while condition is satisfied and it prints '2'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 4: while(i-- >= 0) here i = 2, this statement becomes while(2-- >= 0) Hence the while condition is satisfied and it prints '1'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 5: while(i-- >= 0) here i = 1, this statement becomes while(1-- >= 0) Hence the while condition is satisfied and it prints '0'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 6: while(i-- >= 0) here i = 0, this statement becomes while(0-- >= 0) Hence the while condition is satisfied and it prints '-1'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 7: while(i-- >= 0) here i = -1, this statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.
The output of first while loop is 4,3,2,1,0,-1

Step 2: Then the value of variable i is initialized to '5' Then it prints a new line character(\n).
See the above Loop 1 to Loop 7 .
The output of second while loop is 4,3,2,1,0,-1

Step 3: The third while loop, while(i-- >= 0) here i = -1(because the variable 'i' is decremented to '-1' by previous while loop and it never initialized.). This statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.

Hence the output of the program is
4,3,2,1,0,-1
4,3,2,1,0,-1

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


10.

What will be the output of the program?

#include<stdio.h>
int main()
{
    int i=4;
    switch(i)
    {
        default:
           printf("This is default\n");
        case 1:
           printf("This is case 1\n");
           break;
        case 2:
           printf("This is case 2\n");
           break;
        case 3:
           printf("This is case 3\n");
    }
    return 0;
}

A.
This is default
This is case 1
B.
This is case 3
This is default
C.
This is case 1
This is case 3
D.
This is default

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

In the very begining of switch-case statement default statement is encountered. So, it prints "This is default".

In default statement there is no break; statement is included. So it prints the case 1 statements. "This is case 1".

Then the break; statement is encountered. Hence the program exits from the switch-case block.

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


11.

Point out the compile time error in the program given below.

#include<stdio.h>

int main()
{
    int *x;
    *x=100;
    return 0;
}

A.
Error: invalid assignment for x
B.
Error: suspicious pointer conversion
C.
No error
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

While reading the code there is no error, but upon running the program having an unitialised variable can cause the program to crash (Null pointer assignment).

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


12.

What will be the output of the program (16-bit platform)?

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    p = (int *)malloc(20);
    printf("%d\n", sizeof(p));
    free(p);
    return 0;
}

A.
4
B.
2
C.
8
D.
Garbage value

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Memory Allocation

Discuss about this problem : Discuss in Forum


13.

If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?

A.
.
B.
&
C.
*
D.
->

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


14.

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[])
{
    int i;
    for(i=1; i<argc; i++)
        printf("%c", argv[i][0]);
    return 0;
}

A.
oot
B.
ott
C.
nwh
D.
eoe

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum


15.

Which of the following statement obtains the remainder on dividing 5.5 by 1.3 ?

A.
rem = (5.5 % 1.3)
B.
rem = modf(5.5, 1.3)
C.
rem = fmod(5.5, 1.3)
D.
Error: we can't divide

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

fmod(x,y) - Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs floating point divisions.

Example:


#include <stdio.h>
#include <math.h>

int main ()
{
  printf ("fmod of 5.5 by 1.3 is %lf\n", fmod (5.5, 1.3) );
  return 0;
}

Output:
fmod of 5.5 by 1.3 is 0.300000

Learn more problems on : Floating Point Issues

Discuss about this problem : Discuss in Forum


16.

By default a real number is treated as a

A.
float
B.
double
C.
long double
D.
far double

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

In computing, 'real number' often refers to non-complex floating-point numbers. It include both rational numbers, such as 42 and 3/4, and irrational numbers such as pi = 3.14159265...

When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision and takes double space (8 bytes) than float.

To extend the precision further we can use long double which occupies 10 bytes of memory space.

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


17.

Associativity of an operator is either Left to Right or Right to Left.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Yes, the associativity of an operator is either Left to Right or Right to Left.

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum


18.

What will be the output of the program?

#include<stdio.h>
void fun(int);
typedef int (*pf) (int, int);
int proc(pf, int, int);

int main()
{
    int a=3;
    fun(a);
    return 0;
}
void fun(int n)
{
    if(n > 0)
    {
        fun(--n);
        printf("%d,", n);
        fun(--n);
    }
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


19.

What will be the output of the program?

#include<stdio.h>

int main()
{
    int arr[3] = {2, 3, 4};
    char *p;
    p = arr;
    p = (char*)((int*)(p));
    printf("%d, ", *p);
    p = (int*)(p+1);
    printf("%d", *p);
    return 0;
}

A.
2, 3
B.
2, 0
C.
2, Garbage value
D.
0, 0

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


20.

What will be the output of the program ?

#include<stdio.h>
power(int**);
int main()
{
    int a=5, *aa; /* Address of 'a' is 1000 */
    aa = &a;
    a = power(&aa);
    printf("%d\n", a);
    return 0;
}
power(int **ptr)
{
    int b;
    b = **ptr***ptr;
    return (b);
}

A.
5
B.
25
C.
125
D.
Garbage value

Your Answer: Option (Not Answered)

Correct Answer: Option B

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.