Online C Programming Test - C Programming Test 3

Instruction:

  • This is a FREE online test. Beware of scammers who ask for money to attend this test.
  • Total number of questions: 20.
  • Time allotted: 20 minutes.
  • Each question carries 1 mark; there are no negative marks.
  • DO NOT refresh the page.
  • All the best!

Marks : 2/20


Total number of questions
20
Number of answered questions
0
Number of unanswered questions
20
Test Review : View answers and explanation for this test.

1.
Which statement will you add in the following program to work it correctly?
#include<stdio.h>
int main()
{
    printf("%f\n", log(36.0));
    return 0;
}
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

math.h is a header file in the standard library of C programming language designed for basic mathematical operations.

Declaration syntax: double log(double);


2.
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;
}
2, 3, 4,
2, 2, 2,
3, 3, 3,
4, 4, 4,
Your Answer: Option
(Not Answered)
Correct Answer: Option
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.


3.
What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?
#include<stdio.h>

int main()
{
    int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
    return 0;
}
448, 4, 4
520, 2, 2
1006, 2, 2
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option

4.
Which of the following statements correct about k used in the below statement?
char ****k;
k is a pointer to a pointer to a pointer to a char
k is a pointer to a pointer to a pointer to a pointer to a char
k is a pointer to a char pointer
k is a pointer to a pointer to a char
Your Answer: Option
(Not Answered)
Correct Answer: Option

5.
What does the following declaration mean?
int (*ptr)[10];
ptr is array of pointers to 10 integers
ptr is a pointer to an array of 10 integers
ptr is an array of 10 integers
ptr is an pointer to array
Your Answer: Option
(Not Answered)
Correct Answer: Option

6.
What will be the output of the program in Turbo-C ?
#include<stdio.h>

int main()
{
    int arr[5], i=-1, z;
    while(i<5)
        arr[i]=++i;

    for(i=0; i<5; i++)
        printf("%d, ", arr[i]);

    return 0;
}
1, 2, 3, 4, 5,
-1, 0, 1, 2, 3, 4
0, 1, 2, 3, 4,
0, -1, -2, -3, -4,
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the Turbo-C Compiler (under DOS) output.

Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better.


7.
What will be the output of the program in 16-bit platform (Turbo C under DOS) ?
#include<stdio.h>

int main()
{
    printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0));
    return 0;
}
8, 1, 4
4, 2, 8
4, 2, 4
10, 3, 4
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1:

printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0));

The sizeof function returns the size of the given expression.

sizeof(3.0f) is a floating point constant. The size of float is 4 bytes

sizeof('3') It converts '3' in to ASCII value.. The size of int is 2 bytes

sizeof(3.0) is a double constant. The size of double is 8 bytes

Hence the output of the program is 4,2,8

Note: The above program may produce different output in other platform due to the platform dependency of C compiler.

In Turbo C, 4 2 8. But in GCC, the output will be 4 4 8.


8.
Point out the error in the program?
#include<stdio.h>

int main()
{
    struct emp
    {
        char name[20];
        float sal;
    };
    struct emp e[10];
    int i;
    for(i=0; i<=9; i++)
        scanf("%s %f", e[i].name, &e[i].sal);
    return 0;
}
Error: invalid structure member
Error: Floating point formats not linked
No error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

At run time it will show an error then program will be terminated.

Sample output: Turbo C (Windows)

c:\>myprogram
                                                          
Sample
12.123

scanf : floating point formats not linked 
Abnormal program termination 


9.
A structure can contain similar or dissimilar elements
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

10.
A pointer union CANNOT be created
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

11.
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;
}
25
2
1
5
Your Answer: Option
(Not Answered)
Correct Answer: Option
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'.


12.
Point out the error in the program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    unsigned char;
    FILE *fp;
    fp=fopen("trial", "r");
    if(!fp)
    {
        printf("Unable to open file");
        exit(1);
    }
    fclose(fp);
    return 0;
}
Error: in unsigned char statement
Error: unknown file pointer
No error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

This program tries to open the file trial.txt in read mode. If file not exists or unable to read it prints "Unable to open file" and then terminate the program.

If file exists, it simply close the file and then terminates the program.


13.
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample friday tuesday sunday
/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%c", *++argv[2] );
    return 0;
}
s
f
u
r
Your Answer: Option
(Not Answered)
Correct Answer: Option

14.
What will be the output of the program (sample.c) given below if it is executed from the command line (turbo c under DOS)?
cmd> sample Good Morning
/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%d %s", argc, argv[1]);
    return 0;
}
3 Good
2 Good
Good Morning
3 Morning
Your Answer: Option
(Not Answered)
Correct Answer: Option

15.
Even if integer/float arguments are supplied at command prompt they are treated as strings.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

16.
If the different command line arguments are supplied at different times would the output of the following program change?
#include<stdio.h>

int main(int argc, char **argv)
{
    printf("%d\n", argv[argc]);
    return 0;
}
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

17.
What will be the output of the program?
#include<stdio.h>

typedef struct error {int warning, err, exception;} ERROR;
int main()
{
    ERROR e;
    e.err=1;
    printf("%d\n", e.err);
    return 0;
}
0
1
2
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
What will be the output of the program?
#include<stdio.h>

int main()
{
    const int i=0;
    printf("%d\n", i++);
    return 0;
}
10
11
No output
Error: ++needs a value
Your Answer: Option
(Not Answered)
Correct Answer: Option
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.


19.
Which standard library function will you use to find the last occurance of a character in a string in C?
strnchar()
strchar()
strrchar()
strrchr()
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

strrchr() returns a pointer to the last occurrence of character in a string.

Example:


#include <stdio.h>
#include <string.h>

int main()
{
    char str[30] = "12345678910111213";
    printf("The last position of '2' is %d.\n",
            strrchr(str, '2') - str);
    return 0;
}

Output: The last position of '2' is 14.


20.
Data written into a file using fwrite() can be read back using fscanf()
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

fwrite() - Unformatted write in to a file.
fscanf() - Formatted read from a file.


*** END OF THE TEST ***
Time Left: 00:29:56
Post your test result / feedback here: