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()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit={1, 2, 13};

    printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Note the below statement inside the struct:

int bit1:1; --> 'int' indicates that it is a SIGNED integer.

For signed integers the leftmost bit will be taken for +/- sign.

If you store 1 in 1-bit field:

The left most bit is 1, so the system will treat the value as negative number.

The 2's complement method is used by the system to handle the negative values.

Therefore, the data stored is 1. The 2's complement of 1 is also 1 (negative).

Therefore -1 is printed.


If you store 2 in 4-bits field:

Binary 2: 0010 (left most bit is 0, so system will treat it as positive value)

0010 is 2

Therefore 2 is printed.


If you store 13 in 4-bits field:

Binary 13: 1101 (left most bit is 1, so system will treat it as negative value)

Find 2's complement of 1101:

1's complement of 1101 : 0010
2's complement of 1101 : 0011 (Add 1 to the result of 1's complement)

0011 is 3 (but negative value)

Therefore -3 is printed.

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


2.

We can allocate a 2-Dimensional array dynamically.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


3.

Can I increase the size of dynamically allocated array?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Use realloc(variable_name, value);

Learn more problems on : Memory Allocation

Discuss about this problem : Discuss in Forum


4.

What will be the output of the program?

#include<stdio.h>
int main()
{
    float a = 0.7;
    if(0.7 > a)
        printf("Hi\n");
    else
        printf("Hello\n");
    return 0;
}

A.
Hi
B.
Hello
C.
Hi Hello
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

if(0.7 > a) here a is a float variable and 0.7 is a double constant. The double constant 0.7 is greater than the float variable a. Hence the if condition is satisfied and it prints 'Hi'
Example:

#include<stdio.h>
int main()
{
    float a=0.7;
    printf("%.10f %.10f\n",0.7, a);
    return 0;
}

Output:
0.7000000000 0.6999999881

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


5.

What will be the output of the program in 16 bit platform (Turbo C under DOS)?

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

A.
2
B.
4
C.
vary from compiler
D.
Linker Error : Undefined symbol 'i'

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Linker Error : Undefined symbol 'i'
The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name 'i' is available in any other program with memory space allocated for it. Hence a linker error has occurred.

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


6.

What do the following declaration signify?

void *cmp();

A.
cmp is a pointer to an void type.
B.
cmp is a void type pointer variable.
C.
cmp is a function that return a void pointer.
D.
cmp function returns nothing.

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


7.

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


8.

Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1 ?

A.
rem = 3.14 % 2.1;
B.
rem = modf(3.14, 2.1);
C.
rem = fmod(3.14, 2.1);
D.
Remainder cannot be obtain in floating point division.

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 3.14/2.1 is %lf\n", fmod (3.14,2.1) );
  return 0;
}

Output:
fmod of 3.14/2.1 is 1.040000

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


9.

typedef's have the advantage that they obey scope rules, that is they can be declared local to a function or a block whereas #define's always have a global effect.

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Typedef

Discuss about this problem : Discuss in Forum


10.

Which of the following cannot be checked in a switch-case statement?

A.
Character
B.
Integer
C.
Float
D.
enum

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value.


switch( expression )
{
    case constant-expression1:    statements 1;
    case constant-expression2:    statements 2;    
    case constant-expression3:    statements3 ;
    ...
    ...
    default : statements 4;
}

The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


11.

Bitwise can be used to generate a random number.

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Bitwise Operators

Discuss about this problem : Discuss in Forum


12.

Point out the error in the program?

#include<stdio.h>

int main()
{
    struct emp
    {
        char n[20];
        int age;
    };
    struct emp e1 = {"Dravid", 23};
    struct emp e2 = e1;
    if(e1 == e2)
        printf("The structure are equal");
    return 0;
}

A.
Prints: The structure are equal
B.
Error: Structure cannot be compared using '=='
C.
No output
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Structures, Unions, Enums

Discuss about this problem : Discuss in Forum


13.

What does fp point to in the program ?

#include<stdio.h>

int main()
{
    FILE *fp;
    fp=fopen("trial", "r");
    return 0;
}

A.
The first character in the file
B.
A structure which contains a char pointer which points to the first character of a file.
C.
The name of the file.
D.
The last character in the file.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The fp is a structure which contains a char pointer which points to the first character of a file.

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum


14.

Can we write a function that takes a variable argument list and passes the list to another function?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Variable Number of Arguments

Discuss about this problem : Discuss in Forum


15.

Which of the following statements are correct ?
1: A string is a collection of characters terminated by '\0'.
2: The format specifier %s is used to print a string.
3: The length of the string can be obtained by strlen().
4: The pointer CANNOT work on string.

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Clearly, we know first three statements are correct, but fourth statement is wrong. because we can use pointer on strings. Eg. char *p = "IndiaBIX".

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


16.

Associativity has no role to play unless the precedence of operator is same.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Associativity is only needed when the operators in an expression have the same precedence. Usually + and - have the same precedence.

Consider the expression 7 - 4 + 2. The result could be either (7 - 4) + 2 = 5 or 7 - (4 + 2) = 1. The former result corresponds to the case when + and - are left-associative, the latter to when + and - are right-associative.

Usually the addition, subtraction, multiplication, and division operators are left-associative, while the exponentiation, assignment and conditional operators are right-associative. To prevent cases where operands would be associated with two operators, or no operator at all, operators with the same precedence must have the same associativity.

Learn more problems on : Expressions

Discuss about this problem : Discuss in Forum


17.

What will be the output of the program?

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

int main()
{
    int i=0;
    i++;
    if(i<=5)
    {
        printf("IndiaBIX");
        exit(1);
        main();
    }
    return 0;
}

A.
Prints "IndiaBIX" 5 times
B.
Function main() doesn't calls itself
C.
Infinite loop
D.
Prints "IndiaBIx"

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Step 1: int i=0; The variable i is declared as in integer type and initialized to '0'(zero).

Step 2: i++; Here variable i is increemented by 1. Hence i becomes '1'(one).

Step 3: if(i<=5) becomes if(1 <=5). Hence the if condition is satisfied and it enter into if block statements.

Step 4: printf("IndiaBIX"); It prints "IndiaBIX".

Step 5: exit(1); This exit statement terminates the program execution.

Hence the output is "IndiaBIx".

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


18.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    FILE *fp;
    char ch, str[7];
    fp=fopen("try.c", "r"); /* file 'try.c' contains "This is Nagpur" */
    fseek(fp, 9L, SEEK_CUR);
    fgets(str, 5, fp);
    puts(str);
    return 0;
}

A.
agpur
B.
gpur
C.
Nagp
D.
agpu

Your Answer: Option (Not Answered)

Correct Answer: Option D

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum


19.

What is (void*)0?

A.
Representation of NULL pointer
B.
Representation of void pointer
C.
Error
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


20.

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


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