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.
How would you round off a value from 1.66 to 2.0?
ceil(1.66)
floor(1.66)
roundup(1.66)
roundto(1.66)
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
/* Example for ceil() and floor() functions: */

#include<stdio.h>
#include<math.h>

int main()
{
    printf("\n Result : %f" , ceil(1.44) );
    printf("\n Result : %f" , ceil(1.66) );
 
    printf("\n Result : %f" , floor(1.44) );    
    printf("\n Result : %f" , floor(1.66) );

    return 0;
}
// Output:
// Result : 2.000000
// Result : 2.000000
// Result : 1.000000
// Result : 1.000000

2.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=3;
    switch(i)
    {
        case 1:
            printf("Hello\n");
        case 2:
            printf("Hi\n");
        case 3:
            continue;
        default:
            printf("Bye\n");
    }
    return 0;
}
Error: Misplaced continue
Bye
No output
Hello Hi
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The keyword continue cannot be used in switch case. It must be used in for or while or do while loop. If there is any looping statement in switch case then we can use continue.


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.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=4;
    switch(i)
    {
        default:
           printf("This is default\n");
        case 1:
           printf("This is case 1\n");
           break;
        case 2:
           printf("This is case 2\n");
           break;
        case 3:
           printf("This is case 3\n");
    }
    return 0;
}
This is default
This is case 1
This is case 3
This is default
This is case 1
This is case 3
This is default
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

In the very begining of switch-case statement default statement is encountered. So, it prints "This is default".

In default statement there is no break; statement is included. So it prints the case 1 statements. "This is case 1".

Then the break; statement is encountered. Hence the program exits from the switch-case block.


5.
Which of the following statements are correct about the below program?
#include<stdio.h>
int main()
{
    int i = 10, j = 20;
    if(i = 5) && if(j = 10)
        printf("Have a nice day");
    return 0;
}
Output: Have a nice day
No output
Error: Expression syntax
Error: Undeclared identifier if
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

"Expression syntax" error occur in this line if(i = 5) && if(j = 10).

It should be like if((i == 5) && (j == 10)).


6.
What will be the output of the program?
#include<stdio.h>
int main()
{
    float f=43.20;
    printf("%e, ", f);
    printf("%f, ", f);
    printf("%g", f);
    return 0;
}
4.320000e+01, 43.200001, 43.2
4.3, 43.22, 43.21
4.3e, 43.20f, 43.00
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

printf("%e, ", f); Here '%e' specifies the "Scientific Notation" format. So, it prints the 43.20 as 4.320000e+01.

printf("%f, ", f); Here '%f' specifies the "Decimal Floating Point" format. So, it prints the 43.20 as 43.200001.

printf("%g, ", f); Here '%g' "Use the shorter of %e or %f". So, it prints the 43.20 as 43.2.


7.
Will the printf() statement print the same values for any values of a?
#include<stdio.h>
int main()
{
    float a;
    scanf("%f", &a);
    printf("%f\n", a+a+a);
    printf("%f\n", 3*a);
    return 0;
}
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

8.
Which of the following statements are correct about the program?
#include<stdio.h>

int main()
{
    printf("%p\n", main());
    return 0;
}
It prints garbage values infinitely
Runs infinitely without printing anything
Error: main() cannot be called inside printf()
No Error and print nothing
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

In printf("%p\n", main()); it calls the main() function and then it repeats infinetly, untill stack overflow.


9.
What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?
#include<stdio.h>

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
    printf("%u, %u\n", a+1, &a+1);
    return 0;
}
65474, 65476
65480, 65496
65480, 65488
65474, 65488
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a[3][4] is declared as an integer array having the 3 rows and 4 colums dimensions.

Step 2: printf("%u, %u\n", a+1, &a+1);

The base address(also the address of the first element) of array is 65472.

For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480

Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".

Hence, begining address 65472 + 24 = 65496. So, &a+1 = 65496

Hence the output of the program is 65480, 65496


10.
Which of the following function is correct that finds the length of a string?
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
    {    length++; s++; }
    return (length);
}
int xstrlen(char s)
{
    int length=0;
    while(*s!='\0')
        length++; s++;
    return (length);
}
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
        length++;
    return (length);
}
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
        s++;
    return (length);
}
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Option A is the correct function to find the length of given string.

Example:

#include<stdio.h>

int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
    { length++; s++; }
    return (length);
}

int main()
{
    char d[] = "IndiaBIX";
    printf("Length = %d\n", xstrlen(d));
    return 0;
}

Output: Length = 8


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

int main()
{
    int i=32, j=0x20, k, l, m;
    k=i|j;
    l=i&j;
    m=k^l;
    printf("%d, %d, %d, %d, %d\n", i, j, k, l, m);
    return 0;
}
0, 0, 0, 0, 0
0, 32, 32, 32, 32
32, 32, 32, 32, 0
32, 32, 32, 32, 32
Your Answer: Option
(Not Answered)
Correct Answer: Option

12.
Bitwise & can be used to check if a bit in number is set or not.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    int y=128;
    const int x=y;
    printf("%d\n", x);
    return 0;
}
128
Garbage value
Error
0
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int y=128; The variable 'y' is declared as an integer type and initialized to value "128".

Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with the variable 'y' value.

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

Hence the output of the program is "128"


14.
While defining a variable argument list function we drop the ellipsis(...)?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

15.
Can the fixed arguments passed to the function that accepts variable argument list, occur at the end?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

16.
What do the following declaration signify?
int *ptr[30];
ptr is a pointer to an array of 30 integer pointers.
ptr is a array of 30 pointers to integers.
ptr is a array of 30 integer pointers.
ptr is a array 30 pointers.
Your Answer: Option
(Not Answered)
Correct Answer: Option

17.
What will be the output of the program in DOS (Compiler - Turbo C)?
#include<stdio.h>
double i;

int main()
{
    (int)(float)(char) i;
    printf("%d", sizeof((int)(float)(char)i));
    return 0;
}
1
2
4
8
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Due to the C language is being platform dependent:

In Turbo C (DOS - 16 bit platform), the output will be 2.

But in GCC (Unix/Linux - 32 bit platform), the output will be 4.

18.
Is the following declaration correct?
void(*f)(int, void(*)());
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
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.

19.
Are the following declarations same?
char far *far *scr;
char far far** scr;
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

20.
FILE is a structure suitably typedef'd in "stdio.h".
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
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.


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