Online C Programming Test - C Programming Test - Random

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: 30 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.
Is it true that a global variable may have several declarations, but only one definition?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Yes, In all the global variable declarations, you need to use the keyword extern.


2.
Which of the following cannot be checked in a switch-case statement?
Character
Integer
Float
enum
Your Answer: Option
(Not Answered)
Correct Answer: Option
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.


3.
Which of the following is the correct order of evaluation for the below expression?
z = x + y * z / 4 % 2 - 1
* / % + - =
= * / % + -
/ * % - + =
* % / - + =
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
C uses left associativity for evaluating expressions to break a tie between two operators having same precedence.

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++);
}
9
10
11
8
Your Answer: Option
(Not Answered)
Correct Answer: Option
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.


5.
What is (void*)0?
Representation of NULL pointer
Representation of void pointer
Error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option

6.
What will be the output of the program if the array begins at address 65486?
#include<stdio.h>

int main()
{
    int arr[] = {12, 14, 15, 23, 45};
    printf("%u, %u\n", arr, &arr);
    return 0;
}
65486, 65488
65486, 65486
65486, 65490
65486, 65487
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int arr[] = {12, 14, 15, 23, 45}; The variable arr is declared as an integer array and initialized.

Step 2: printf("%u, %u\n", arr, &arr); Here,

The base address of the array is 65486.

=> arr, &arr is pointing to the base address of the array arr.

Hence the output of the program is 65486, 65486


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

int main()
{
    enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
    printf("%d, %d, %d, %d, %d, %d\n", ++MON, TUE, WED, THU, FRI, SAT);
    return 0;
}
-1, 0, 1, 2, 3, 4
Error
0, 1, 6, 3, 4, 5
0, 0, 6, 7, 8, 9
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Because ++ or -- cannot be done on enum value.

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

int main()
{
    struct bits
    {
        float f:2;
    }bit;

    printf("%d\n", sizeof(bit));
    return 0;
}
4
2
Error: cannot set bit field for float
Error: Invalid member access in structure
Your Answer: Option
(Not Answered)
Correct Answer: Option

9.
Nested unions are allowed
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

10.
Will the following declaration work?
typedef struct s
{
    int a;
    float b;
}s;
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

11.
What is the purpose of "rb" in fopen() function used below in the code?
FILE *fp;
fp = fopen("source.txt", "rb");
open "source.txt" in binary mode for reading
open "source.txt" in binary mode for reading and writing
Create a new file "source.txt" for reading and writing
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The file source.txt will be opened in the binary mode.


12.
Consider the following program and what will be content of t?
#include<stdio.h>

int main()
{
    FILE *fp;
    int t;
    fp = fopen("DUMMY.C", "w");
    t = fileno(fp);
    printf("%d\n", t);
    return 0;
}
size of "DUMMY.C" file
The handle associated with "DUMMY.C" file
Garbage value
Error in fileno()
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

fp = fopen("DUMMY.C", "w"); A file DUMMY.C is opened in write mode and returns the file pointer to fp

t = fileno(fp); returns the handle for the fp stream and it stored in the variable t

printf("%d\n", t); It prints the handle number.


13.
If an unsigned int is 2 bytes wide then, What will be the output of the program ?
#include<stdio.h>

int main()
{
    unsigned int m = 32;
    printf("%x\n", ~m);
    return 0;
}
ffff
0000
ffdf
ddfd
Your Answer: Option
(Not Answered)
Correct Answer: Option

14.
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.


15.
malloc() returns a float pointer if memory is allocated for storing float's and a double pointer if memory is allocated for storing double's.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

16.
Which header file should you include, if you are going to develop a function, which can accept variable number of arguments?
varagrg.h
stdlib.h
stdio.h
stdarg.h
Your Answer: Option
(Not Answered)
Correct Answer: Option

17.
Is the following declaration correct?
typedef *void (*pfun)(**int, *float);
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
Does there any function exist to convert the int or float to a string?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

1. itoa() converts an integer to a string.
2. ltoa() converts a long to a string.
3. ultoa() converts an unsigned long to a string.
4. sprintf() sends formatted output to a string, so it can be used to convert any type of values to string type.

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

int main(void)
{
   int   num1 = 12345;
   float num2 = 5.12;
   char str1[20];
   char str2[20];

   itoa(num1, str1, 10); /* 10 radix value */
   printf("integer = %d string = %s \n", num1, str1);

   sprintf(str2, "%f", num2);
   printf("float = %f string = %s", num2, str2);

   return 0;
}

// Output:
// integer = 12345 string = 12345
// float = 5.120000 string = 5.120000


19.
What is the purpose of fflush() function.
flushes all streams and specified streams.
flushes only specified stream.
flushes input/output buffer.
flushes file buffer.
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
"fflush()" flush any buffered output associated with filename, which is either a file opened for writing or a shell command for redirecting output to a pipe or coprocess.

Example:
fflush(FilePointer);
fflush(NULL); flushes all streams.


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

int main()
{
    int i;
    i = scanf("%d %d", &i, &i);
    printf("%d\n", i);
    return 0;
}
1
2
Garbage value
Error: cannot assign scanf to variable
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
scanf() returns the number of variables to which you are provding the input.

i = scanf("%d %d", &i, &i); Here Scanf() returns 2. So i = 2.

printf("%d\n", i); Here it prints 2.


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