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.

Is the following declaration correct?
char far *far *ptr;

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

ptr is a far pointer to a far pointer to a char. (or) ptr contains a far address of a far pointer to a char

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


2.

va_list is an array that holds information needed by va_arg and va_end

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


3.

A file written in text mode can be read back in binary mode.

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

The difference is that text files contain lines (or records) of text and each of these has an end-of-line marker automatically appended to the end of it whenever you indicate that you have reached the end of a line.

Binary files are not broken up into separate lines or records so the end-of line marker is not written when writing to a binary file.

So, we cannot read the correct the data in binary mode.

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum


4.

What will be the output of the program?

#include<stdio.h>

int main()
{
    int fun(int);
    int i = fun(10);
    printf("%d\n", --i);
    return 0;
}
int fun(int i)
{
   return (i++);
}

A.
9
B.
10
C.
11
D.
8

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

Step 1: int fun(int); Here we declare the prototype of the function fun().

Step 2: int i = fun(10); The variable i is declared as an integer type and the result of the fun(10) will be stored in the variable i.

Step 3: int fun(int i){ return (i++); } Inside the fun() we are returning a value return(i++). It returns 10. because i++ is the post-increement operator.

Step 4: Then the control back to the main function and the value 10 is assigned to variable i.

Step 5: printf("%d\n", --i); Here --i denoted pre-increement. Hence it prints the value 9.

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


5.

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>
#include<stdlib.h>

int main(int argc, char **argv)
{
    int i;
    for(i=1; i<=3; i++)
        printf("%u\n", &argv[i]);
    return 0;
}

If the first value printed by the above program is 65517, what will be the rest of output?

A.
65525 65531
B.
65519 65521
C.
65517 65517
D.
65521 65525

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum


6.

Point out the error in the program

#include<stdio.h>

int main()
{
    int a[] = {10, 20, 30, 40, 50};
    int j;
    for(j=0; j<5; j++)
    {
        printf("%d\n", a);
        a++;
    }
    return 0;
}

A.
Error: Declaration syntax
B.
Error: Expression syntax
C.
Error: LValue required
D.
Error: Rvalue required

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


7.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    char *p;
    p="hello";
    printf("%s\n", *&*&p);
    return 0;
}

A.
llo
B.
hello
C.
ello
D.
h

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Pointers

Discuss about this problem : Discuss in Forum


8.

What will be the output of the program if value 25 given to scanf()?

#include<stdio.h>

int main()
{
    int i;
    printf("%d\n", scanf("%d", &i));
    return 0;
}

A.
25
B.
2
C.
1
D.
5

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The scanf function returns the number of input is given.

printf("%d\n", scanf("%d", &i)); The scanf function returns the value 1(one).

Therefore, the output of the program is '1'.

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum


9.

Does the data type of all elements in the union will be same.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


10.

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


11.

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


12.

Point out the error, if any in the while loop.

#include<stdio.h>
int main()
{
    void fun();
    int i = 1;
    while(i <= 5)
    {
        printf("%d\n", i);
        if(i>2)
            goto here;
    }
return 0;
}
void fun()
{
    here:
    printf("It works");
}

A.
No Error: prints "It works"
B.
Error: fun() cannot be accessed
C.
Error: goto cannot takeover control to other function
D.
No error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

A label is used as the target of a goto statement, and that label must be within the same function as the goto statement.

Syntax: goto <identifier> ;
Control is unconditionally transferred to the location of a local label specified by <identifier>.
Example:


#include <stdio.h>
int main()
{
    int i=1;
    while(i>0)
    {
        printf("%d", i++);
        if(i==5)
          goto mylabel;
    }
    mylabel:
    return 0;
}
 

Output: 1,2,3,4

Learn more problems on : Control Instructions

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.

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


15.

Point out the error in the program.

#include<stdio.h>
#define MAX 128

int main()
{
    char mybuf[] = "India";
    char yourbuf[] = "BIX";
    char *const ptr = mybuf;
    *ptr = 'a';
    ptr = yourbuf;
    return 0;
}

A.
Error: unknown pointer conversion
B.
Error: cannot convert ptr const value
C.
No error
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

Step 1: char mybuf[] = "India"; The variable mybuff is declared as an array of characters and initialized with string "India".

Step 2: char yourbuf[] = "BIX"; The variable yourbuf is declared as an array of characters and initialized with string "BIX".

Step 3: char *const ptr = mybuf; Here, ptr is a constant pointer, which points at a char.

The value at which ptr it points is not a constant; it will not be an error to modify the pointed character; There will be an error only to modify the pointer itself.

Step 4: *ptr = 'a'; The value of ptr is assigned to 'a'.

Step 5: ptr = yourbuf; Here, we are changing the pointer itself, this will result in the error "cannot modify a const object".

Learn more problems on : Const

Discuss about this problem : Discuss in Forum


16.

Point out the error in the program.

#include<stdio.h>
const char *fun();

int main()
{
    *fun() = 'A';
    return 0;
}
const char *fun()
{
    return "Hello";
}

A.
Error: RValue required
B.
Error: Lvalue required
C.
Error: fun() returns a pointer const character which cannot be modified
D.
No error

Your Answer: Option (Not Answered)

Correct Answer: Option C

Learn more problems on : Const

Discuss about this problem : Discuss in Forum


17.

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


18.

A preprocessor directive is a message from compiler to a linker.

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

FALSE

Example: #define symbol replacement

When the preprocessor encounters #define directive, it replaces any occurrence of symbol in the rest of the code by replacement. This replacement can be an statement or expression or a block or simple text.

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum


19.

What do the following declaration signify?

char **argv;

A.
argv is a pointer to pointer.
B.
argv is a pointer to a char pointer.
C.
argv is a function pointer.
D.
argv is a member of function pointer.

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


20.

What will be the output of the program?

#include<stdio.h>
int main()
{
    int a = 500, b = 100, c;
    if(!a >= 400)
        b = 300;
    c = 200;
    printf("b = %d c = %d\n", b, c);
    return 0;
}

A.
b = 300 c = 200
B.
b = 100 c = garbage
C.
b = 300 c = garbage
D.
b = 100 c = 200

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Initially variables a = 500, b = 100 and c is not assigned.

Step 1: if(!a >= 400)
Step 2: if(!500 >= 400)
Step 3: if(0 >= 400)
Step 4: if(FALSE) Hence the if condition is failed.
Step 5: So, variable c is assigned to a value '200'.
Step 6: printf("b = %d c = %d\n", b, c); It prints value of b and c.
Hence the output is "b = 100 c = 200"

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


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