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.
Which of the following is the correct usage of conditional operators used in C?
a>b ? c=30 : c=40;
a>b ? c=30;
max = a>b ? a>c?a:c:b>c?b:c
return (a>b)?(a:b)
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Option A: assignment statements are always return in paranthesis in the case of conditional operator. It should be a>b? (c=30):(c=40);

Option B: it is syntatically wrong.

Option D: syntatically wrong, it should be return(a>b ? a:b);

Option C: it uses nested conditional operator, this is logic for finding greatest number out of three numbers.


2.
Will the expression *p = p be disallowed by the compiler?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Because, here even though the value of p is accessed twice it is used to modify two different objects p and *p

3.
Are the following two statement same?
1. a <= 20 ? (b = 30): (c = 30);
2. (a <=20) ? b : (c = 30);
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

No, the expressions 1 and 2 are not same.

1. a <= 20 ? (b = 30) : (c = 30); This statement can be rewritten as,


if(a <= 20)
{
    b = 30;
}
else
{
    c = 30;
}

2. (a <=20) ? b : (c = 30); This statement can be rewritten as,


if(a <= 20)
{
    //Nothing here
}
else
{
    c = 30;
}


4.
We want to round off x, a float, to an int value, The correct way to do is
y = (int)(x + 0.5)
y = int(x + 0.5)
y = (int)x + 0.5
y = (int)((int)x + 0.5)
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Rounding off a value means replacing it by a nearest value that is approximately equal or smaller or greater to the given number.

y = (int)(x + 0.5); here x is any float value. To roundoff, we have to typecast the value of x by using (int)

Example:


#include <stdio.h>

int main ()
{
  float x = 3.6;
  int y = (int)(x + 0.5);
  printf ("Result = %d\n", y );
  return 0;
}

Output:
Result = 4.


5.
What is the notation for following functions?
1.  int f(int a, float b)
    {
        /* Some code */
    }

2.  int f(a, b)
    int a; float b;
    {
        /* Some code */
    }
1. KR Notation
2. ANSI Notation
1. Pre ANSI C Notation
2. KR Notation
1. ANSI Notation
2. KR Notation
1. ANSI Notation
2. Pre ANSI Notation
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
KR Notation means Kernighan and Ritche Notation.

6.
Names of functions in two different files linked together must be unique
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

True, If two function are declared in a same name, it gives "Error: Multiple declaration of function_name())".


7.
Would the following typedef work?
typedef #include l;
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Because typedef goes to work after preprocessing.

8.
Which of the following statements mentioning the name of the array begins DOES NOT yield the base address?
1: When array name is used with the sizeof operator.
2: When array name is operand of the & operator.
3: When array name is passed to scanf() function.
4: When array name is passed to printf() function.
A
A, B
B
B, D
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The statement 1 and 2 does not yield the base address of the array. While the scanf() and printf() yields the base address of the array.


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

int main()
{
    char str = "IndiaBIX";
    printf("%s\n", str);
    return 0;
}
Error
IndiaBIX
Base address of str
No output
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The line char str = "IndiaBIX"; generates "Non portable pointer conversion" error.

To eliminate the error, we have to change the above line to

char *str = "IndiaBIX"; (or) char str[] = "IndiaBIX";

Then it prints "IndiaBIX".


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

int main()
{
    float a=3.15529;
    printf("%2.1f\n", a);
    return 0;
}
3.00
3.15
3.2
3
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

float a=3.15529; The variable a is declared as an float data type and initialized to value 3.15529;

printf("%2.1f\n", a); The precision specifier tells .1f tells the printf function to place only one number after the .(dot).

Hence the output is 3.2


11.
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample one two three
/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    int i=0;
    i+=strlen(argv[1]);
    while(i>0)
    {
        printf("%c", argv[1][--i]);
    }
    return 0;
}
three two one
owt
eno
eerht
Your Answer: Option
(Not Answered)
Correct Answer: Option

12.
Bitwise & can be used to check if more than one bit in a number is on.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

13.
What is x in the following program?
#include<stdio.h>

int main()
{
    typedef char (*(*arrfptr[3])())[10];
    arrfptr x;
    return 0;
}
x is a pointer
x is an array of three pointer
x is an array of three function pointers
Error in x declaration
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    typedef int LONG;
    LONG a=4;
    LONG b=68;
    float c=0;
    c=b;
    b+=a;
    printf("%d,", b);
    printf("%f\n", c);
    return 0;
}
72, 68.000000
72.000000, 68
68.000000, 72.000000
68, 72.000000
Your Answer: Option
(Not Answered)
Correct Answer: Option

15.
Assume integer is 2 bytes wide. How many bytes will be allocated for the following code?
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int (*p)[MAXCOL];
    p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
    return 0;
}
56 bytes
128 bytes
24 bytes
12 bytes
Your Answer: Option
(Not Answered)
Correct Answer: Option

16.
Which of the following statement is correct prototype of the malloc() function in c ?
int* malloc(int);
char* malloc(char);
unsigned int* malloc(unsigned int);
void* malloc(size_t);
Your Answer: Option
(Not Answered)
Correct Answer: Option

17.
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void fun1(char, int, int *, float *, char *);
void fun2(char ch, ...);
void (*p1)(char, int, int *, float *, char *);
void (*p2)(char ch, ...);

int main()
{
    char ch='A'; int i=10;
    float f=3.14; char *p="Hello";
    p1=fun1;
    p2=fun2;
    (*p1)(ch, i, &i, &f, p);
    (*p2)(ch, i, &i, &f, p);
    return 0;
}
void fun1(char ch, int i, int *pi, float *pf, char *p)
{
    printf("%c %d %d %f %s \n", ch, i, *pi, *pf, p);
}
void fun2(char ch, ...)
{
    int i, *pi; float *pf; char *p;
    va_list list;
    printf("%c ", ch);
    va_start(list, ch);
    i = va_arg(list, int);
    printf("%d ", i);
    
    pi = va_arg(list, int*);
    printf("%d ", *pi);
    pf = va_arg(list, float*);
    printf("%f ", *pf);
    p = va_arg(list, char *);
    printf("%s", p);
}
A 10 3.14
A 10 3.14
A 10 10 3.140000 Hello
A 10 10 3.140000 Hello
A 10 Hello
A 10 Hello
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
What will be the output of the program?
#include<stdio.h>
#include<stdarg.h>
void display(int num, ...);

int main()
{
    display(4, 'A', 'B', 'C', 'D');
    return 0;
}
void display(int num, ...)
{
    char c, c1; int j;
    va_list ptr, ptr1;
    va_start(ptr, num);
    va_start(ptr1, num);
    for(j=1; j<=num; j++)
    {
        c = va_arg(ptr, int);
        printf("%c", c);
        c1 = va_arg(ptr1, int);
        printf("%d\n", c1);
    }
}
A, A
B, B
C, C
D, D
A, a
B, b
C, c
D, d
A, 65
B, 66
C, 67
D, 68
A, 0
B, 0
C, 0
C, 0
Your Answer: Option
(Not Answered)
Correct Answer: Option

19.
Is it necessary that in a function which accepts variable argument list there should be at least be one fixed argument?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

20.
What will be the output of the program?
#include<stdio.h>
typedef void v;
typedef int i;

int main()
{
    v fun(i, i);
    fun(2, 3);
    return 0;
}
v fun(i a, i b)
{
    i s=2;
    float i;
    printf("%d,", sizeof(i));
    printf(" %d", a*b*s);
}
2, 8
4, 8
2, 4
4, 12
Your Answer: Option
(Not Answered)
Correct Answer: Option

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