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.

Which of the following is correct about err used in the declaration given below?

 typedef enum error { warning, test, exception } err;

A.
It is a typedef for enum error.
B.
It is a variable of type enum error.
C.
The statement is erroneous.
D.
It is a structure.

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

A typedef gives a new name to an existing data type.
So err is a new name for enum error.

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


2.

FILE is a structure suitably typedef'd in "stdio.h".

A.
True
B.
False

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

FILE - a structure containing the information about a file or text stream needed to perform input or output operations on it, including:
=> a file descriptor, the current stream position,
=> an end-of-file indicator,
=> an error indicator,
=> a pointer to the stream's buffer, if applicable

fpos_t - a non-array type capable of uniquely identifying the position of every byte in a file.
size_t - an unsigned integer type which is the type of the result of the sizeof operator.

Learn more problems on : Library Functions

Discuss about this problem : Discuss in Forum


3.

Which of the following is TRUE about argv?

A.
It is an array of character pointers
B.
It is a pointer to an array of character pointers
C.
It is an array of strings
D.
None of above

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Command Line Arguments

Discuss about this problem : Discuss in Forum


4.

What will be the output of the program ?

#include<stdio.h>

int main()
{
    char str[] = "Nagpur";
    str[0]='K';
    printf("%s, ", str);
    str = "Kanpur";
    printf("%s", str+1);
    return 0;
}

A.
Kagpur, Kanpur
B.
Nagpur, Kanpur
C.
Kagpur, anpur
D.
Error

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

The statement str = "Kanpur"; generates the LVALUE required error. We have to use strcpy function to copy a string.

To remove error we have to change this statement str = "Kanpur"; to strcpy(str, "Kanpur");

The program prints the string "anpur"

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


5.

What will be the output of the program?

#include<stdio.h>
#define PRINT(i) printf("%d,",i)

int main()
{
    int x=2, y=3, z=4;
    PRINT(x);
    PRINT(y);
    PRINT(z);
    return 0;
}

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

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The macro PRINT(i) print("%d,", i); prints the given variable value in an integer format.

Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized to 2, 3, 4 respectively.

Step 2: PRINT(x); becomes printf("%d,",x). Hence it prints '2'.

Step 3: PRINT(y); becomes printf("%d,",y). Hence it prints '3'.

Step 4: PRINT(z); becomes printf("%d,",z). Hence it prints '4'.

Hence the output of the program is 2, 3, 4.

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum


6.

What will be the output of the program?

#include<stdio.h>
#define MIN(x, y) (x<y)? x : y;
int main()
{
    int x=3, y=4, z;
    z = MIN(x+y/2, y-1);
    if(z > 0)
        printf("%d\n", z);
    return 0;
}

A.
3
B.
4
C.
0
D.
No output

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

The macro MIN(x, y) (x<y)? x : y; returns the smallest value from the given two numbers.

Step 1: int x=3, y=4, z; The variable x, y, z are declared as an integer type and the variable x, y are initialized to value 3, 4 respectively.

Step 2: z = MIN(x+y/2, y-1); becomes,

=> z = (x+y/2 < y-1)? x+y/2 : y - 1;

=> z = (3+4/2 < 4-1)? 3+4/2 : 4 - 1;

=> z = (3+2 < 4-1)? 3+2 : 4 - 1;

=> z = (5 < 3)? 5 : 3;

The macro return the number 3 and it is stored in the variable z.

Step 3: if(z > 0) becomes if(3 > 0) here the if condition is satisfied. It executes the if block statements.

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

Hence the output of the program is 3

Learn more problems on : C Preprocessor

Discuss about this problem : Discuss in Forum


7.

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


8.

Suppose a program is divided into three files f1, f2 and f3, and a variable is defined in the file f1 but used in files f2 and f3. In such a case would we need the extern declaration for the variables in the files f2 and f3?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Declarations and Initializations

Discuss about this problem : Discuss in Forum


9.

The way the break is used to take control out of switch and continue to take control of the beginning of the switch?

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option B

Explanation:

continue can work only with loops and not with switch

Learn more problems on : Control Instructions

Discuss about this problem : Discuss in Forum


10.

If the two strings are identical, then strcmp() function returns

A.
-1
B.
1
C.
0
D.
Yes

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

Declaration: strcmp(const char *s1, const char*s2);

The strcmp return an int value that is

if s1 < s2 returns a value < 0

if s1 == s2 returns 0

if s1 > s2 returns a value > 0

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


11.

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


12.

Which of the following statement is correct?

A.
strcmp(s1, s2) returns a number less than 0 if s1>s2
B.
strcmp(s1, s2) returns a number greater than 0 if s1<s2
C.
strcmp(s1, s2) returns 0 if s1==s2
D.
strcmp(s1, s2) returns 1 if s1==s2

Your Answer: Option (Not Answered)

Correct Answer: Option C

Explanation:

The strcmp return an int value that is

if s1 < s2 returns a value < 0

if s1 == s2 returns 0

if s1 > s2 returns a value > 0

From the above statements, that the third statement is only correct.

Learn more problems on : Strings

Discuss about this problem : Discuss in Forum


13.

Which of the following statement is correct about the program?

#include<stdio.h>

int main()
{
    FILE *fp;
    char ch;
    int i=1;
    fp = fopen("myfile.c", "r");
    while((ch=getc(fp))!=EOF)
    {
        if(ch == '\n')
            i++;
    }
    fclose(fp);
    return 0;
}

A.
The code counts number of characters in the file
B.
The code counts number of words in the file
C.
The code counts number of blank lines in the file
D.
The code counts number of lines in the file

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

This program counts the number of lines in the file myfile.c by counting the character '\n' in that file.

Learn more problems on : Input / Output

Discuss about this problem : Discuss in Forum


14.

Is the following declaration correct?
void(*f)(int, void(*)());

A.
Yes
B.
No

Your Answer: Option (Not Answered)

Correct Answer: Option A

Explanation:

f is a pointer to a function which returns nothing and receives as its parameter an integer and a pointer to a function which receives nothing and returns nothing.

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


15.

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


16.

Nested unions are allowed

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


17.

What will be the output of the program under DOS?

#include<stdio.h>

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

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

Your Answer: Option (Not Answered)

Correct Answer: Option B

Learn more problems on : Complicated Declarations

Discuss about this problem : Discuss in Forum


18.

What will be the output of the program?

#include<stdio.h>

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

A.
10
B.
11
C.
No output
D.
Error: ++needs a value

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

This program will show an error "Cannot modify a const object".

Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value of '0'(zero).

Step 2: printf("%d\n", i++); Here the variable 'i' is increemented by 1(one). This will create an error "Cannot modify a const object".

Because, we cannot modify a const variable.

Learn more problems on : Const

Discuss about this problem : Discuss in Forum


19.

What will be the output of the program?

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

int main()
{
    int *p;
    p = (int *)malloc(20); /* Assume p has address of 1314 */
    free(p);
    printf("%u", p);
    return 0;
}

A.
1314
B.
Garbage value
C.
1316
D.
Random address

Your Answer: Option (Not Answered)

Correct Answer: Option A

Learn more problems on : Memory Allocation

Discuss about this problem : Discuss in Forum


20.

What will be the output of the program?

#include<stdio.h>
int func1(int);

int main()
{
    int k=35;
    k = func1(k=func1(k=func1(k)));
    printf("k=%d\n", k);
    return 0;
}
int func1(int k)
{
    k++;
    return k;
}

A.
k=35
B.
k=36
C.
k=37
D.
k=38

Your Answer: Option (Not Answered)

Correct Answer: Option D

Explanation:

Step 1: int k=35; The variable k is declared as an integer type and initialized to 35.

Step 2: k = func1(k=func1(k=func1(k))); The func1(k) increement the value of k by 1 and return it. Here the func1(k) is called 3 times. Hence it increements value of k = 35 to 38. The result is stored in the variable k = 38.

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

Learn more problems on : Functions

Discuss about this problem : Discuss in Forum


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