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.
What is the output of the program in Turbo C (in DOS 16-bit OS)?
#include<stdio.h>
int main()
{
    char *s1;
    char far *s2;
    char huge *s3;
    printf("%d, %d, %d\n", sizeof(s1), sizeof(s2), sizeof(s3));
    return 0;
}
2, 4, 6
4, 4, 2
2, 4, 4
2, 2, 2
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Any pointer size is 2 bytes. (only 16-bit offset)
So, char *s1 = 2 bytes.
So, char far *s2; = 4 bytes.
So, char huge *s3; = 4 bytes.
A far, huge pointer has two parts: a 16-bit segment value and a 16-bit offset value.

Since C is a compiler dependent language, it may give different output in other platforms. The above program works fine in Windows (TurboC), but error in Linux (GCC Compiler).


2.
If the definition of the external variable occurs in the source file before its use in a particular function, then there is no need for an extern declaration in the function.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

True, When a function is declared inside the source file, that function(local function) get a priority than the extern function. So there is no need to declare a function as extern inside the same source file.


3.
What will be the output of the program, if a short int is 2 bytes wide?
#include<stdio.h>
int main()
{
    short int i = 0;
    for(i<=5 && i>=-1; ++i; i>0)
        printf("%u,", i);
    return 0;
}
1 ... 65535
Expression syntax error
No output
0, 1, 2, 3, 4, 5
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

for(i<=5 && i>=-1; ++i; i>0) so expression i<=5 && i>=-1 initializes for loop. expression ++i is the loop condition. expression i>0 is the increment expression.

In for( i <= 5 && i >= -1; ++i; i>0) expression i<=5 && i>=-1 evaluates to one.

Loop condition always get evaluated to true. Also at this point it increases i by one.

An increment_expression i>0 has no effect on value of i.so for loop get executed till the limit of integer (ie. 65535)


4.
In which order do the following gets evaluated
1. Relational
2. Arithmetic
3. Logical
4. Assignment
2134
1234
4321
3214
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

2. Arithmetic operators: *, /, %, +, -
1. Relational operators: >, <, >=, <=, ==, !=
3. Logical operators : !, &&, ||
4. Assignment operators: =


5.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=-3, j=2, k=0, m;
    m = ++i && ++j && ++k;
    printf("%d, %d, %d, %d\n", i, j, k, m);
    return 0;
}
-2, 3, 1, 1
2, 3, 1, 2
1, 2, 3, 1
3, 3, 1, 2
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.

Step 2: m = ++i && ++j && ++k;
becomes m = -2 && 3 && 1;
becomes m = TRUE && TRUE; Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.

Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of i,j,k are increemented by '1'(one).

Hence the output is "-2, 3, 1, 1".


6.
What are the different types of real data type in C ?
float, double
short int, double, long int
float, double, long double
double, long int, float
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
The floating point data types are called real data types. Hence float, double, and long double are real data types.

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

int addmult(int ii, int jj)
{
    int kk, ll;
    kk = ii + jj;
    ll = ii * jj;
    return (kk, ll);
}

int main()
{
    int i=3, j=4, k, l;
    k = addmult(i, j);
    l = addmult(i, j);
    printf("%d, %d\n", k, l);
    return 0;
}
12, 12
7, 7
7, 12
12, 7
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int i=3, j=4, k, l; The variables i, j, k, l are declared as an integer type and variable i, j are initialized to 3, 4 respectively.

The function addmult(i, j); accept 2 integer parameters.

Step 2: k = addmult(i, j); becomes k = addmult(3, 4)

In the function addmult(). The variable kk, ll are declared as an integer type int kk, ll;

kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.

ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.

return (kk, ll); It returns the value of variable ll only.

The value 12 is stored in variable 'k'.

Step 3: l = addmult(i, j); becomes l = addmult(3, 4)

kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.

ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.

return (kk, ll); It returns the value of variable ll only.

The value 12 is stored in variable 'l'.

Step 4: printf("%d, %d\n", k, l); It prints the value of k and l

Hence the output is "12, 12".


8.
In a function two return statements should never occur.
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

No, In a function two return statements can occur but not successively.

Example:


#include <stdio.h>
int mul(int, int); /* Function prototype */

int main()
{
    int a = 0, b = 3, c;
    c = mul(a, b);
    printf("c = %d\n", c);
    return 0;
}

/* Two return statements in the mul() function */
int mul(int a, int b)
{
   if(a == 0 || b == 0)
   {
        return 0;
   }
   else
   {
        return (a * b);
   }
}

Output:
c = 0


9.
The following program reports an error on compilation.
#include<stdio.h>
int main()
{
    float i=10, *j;
    void *k;
    k=&i;
    j=k;
    printf("%f\n", *j);
    return 0;
}
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
This program will NOT report any error. (Tested in Turbo C under DOS and GCC under Linux)

The output: 10.000000

10.
What will be the output of the program ?
#include<stdio.h>
void fun(int **p);

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
    int *ptr;
    ptr = &a[0][0];
    fun(&ptr);
    return 0;
}
void fun(int **p)
{
    printf("%d\n", **p);
}
1
2
3
4
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0}; The variable a is declared as an multidimensional integer array with size of 3 rows 4 columns.

Step 2: int *ptr; The *ptr is a integer pointer variable.

Step 3: ptr = &a[0][0]; Here we are assigning the base address of the array a to the pointer variable *ptr.

Step 4: fun(&ptr); Now, the &ptr contains the base address of array a.

Step 4: Inside the function fun(&ptr); The printf("%d\n", **p); prints the value '1'.

because the *p contains the base address or the first element memory address of the array a (ie. a[0])

**p contains the value of *p memory location (ie. a[0]=1).

Hence the output of the program is '1'


11.
Which of the following statements are correct about an array?
1: The array int num[26]; can store 26 elements.
2: The expression num[1] designates the very first element in the array.
3: It is necessary to initialize the array at the time of declaration.
4: The declaration num[SIZE] is allowed if SIZE is a macro.
1
1,4
2,3
2,4
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

1. The array int num[26]; can store 26 elements. This statement is true.

2. The expression num[1] designates the very first element in the array. This statement is false, because it designates the second element of the array.

3. It is necessary to initialize the array at the time of declaration. This statement is false.

4. The declaration num[SIZE] is allowed if SIZE is a macro. This statement is true, because the MACRO just replaces the symbol SIZE with given value.

Hence the statements '1' and '4' are correct statements.


12.
Is there any difference int the following declarations?
int fun(int arr[]);
int fun(int arr[2]);
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

No, both the statements are same. It is the prototype for the function fun() that accepts one integer array as an parameter and returns an integer value.


13.
How will you free the allocated memory ?
remove(var-name);
free(var-name);
delete(var-name);
dalloc(var-name);
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit;
    printf("%d\n", sizeof(bit));
    return 0;
}
1
2
4
9
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Since C is a compiler dependent language, in Turbo C (DOS) the output will be 2, but in GCC (Linux) the output will be 4.


15.
Can we have an array of bit fields?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    printf("%%%%\n");
    return 0;
}
%%%%%
%%
No output
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option

17.
The first argument to be supplied at command-line must always be count of total arguments.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
Bitwise can be used to perform addition and subtraction.
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    const int x;
    x=128;
    printf("%d\n", x);
    return 0;
}
Error: unknown data type const int
Error: const variable have been initialised when declared.
Error: stack overflow in x
No error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

A const variable has to be initialized when it is declared. later assigning the value to the const variable will result in an error "Cannot modify the const object".

Hence Option B is correct


20.
Can I increase the size of statically allocated array?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

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