Online C Programming Test - C Programming Test 4

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.
How many times "IndiaBIX" is get printed?
#include<stdio.h>
int main()
{
    int x;
    for(x=-1; x<=10; x++)
    {
        if(x < 5)
            continue;
        else
            break;
        printf("IndiaBIX");
    }
    return 0;
}
Infinite times
11 times
0 times
10 times
Your Answer: Option
(Not Answered)
Correct Answer: Option

2.
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;
}
1, 2, 0, 1
-3, 2, 0, 1
-2, 3, 0, 1
2, 3, 1, 1
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) || ++k;
becomes m = TRUE || ++k;.
(++k) is not executed because (-2 && 3) alone return 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 are increemented by '1'(one).

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


3.
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".


4.
Will the following program print the message infinite number of times?
#include<stdio.h>
#define INFINITELOOP while(1)

int main()
{
    INFINITELOOP
    printf("IndiaBIX");
    return 0;
}
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Yes, the program prints "IndiaBIX" and runs infinitely.

The macro INFINITELOOP while(1) replaces the text 'INFINITELOOP' by 'while(1)'

In the main function, while(1) satisfies the while condition and it prints "IndiaBIX". Then it comes to while(1) and the loop runs infinitely.


5.
If the size of integer is 4bytes, What will be the output of the program?
#include<stdio.h>

int main()
{
    int arr[] = {12, 13, 14, 15, 16};
    printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0]));
    return 0;
}
10, 2, 4
20, 4, 4
16, 2, 2
20, 2, 2
Your Answer: Option
(Not Answered)
Correct Answer: Option

6.
What will be the output of the program ?
#include<stdio.h>
int *check(static int, static int);

int main()
{
    int *c;
    c = check(10, 20);
    printf("%d\n", c);
    return 0;
}
int *check(static int i, static int j)
{
    int *p, *q;
    p = &i;
    q = &j;
    if(i >= 45)
        return (p);
    else
        return (q);
}
10
20
Error: Non portable pointer conversion
Error: cannot use static for function parameters
Your Answer: Option
(Not Answered)
Correct Answer: Option

7.
In the following program add a statement in the function fact() such that the factorial gets stored in j.
#include<stdio.h>
void fact(int*);

int main()
{
    int i=5;
    fact(&i);
    printf("%d\n", i);
    return 0;
}
void fact(int *j)
{
    static int s=1;
    if(*j!=0)
    {
        s = s**j;
        *j = *j-1;
        fact(j);
        /* Add a statement here */
    }
}
j=s;
*j=s;
*j=&s;
&j=s;
Your Answer: Option
(Not Answered)
Correct Answer: Option

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


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

int main()
{
    int i;
    char a[] = "\0";
    if(printf("%s", a))
        printf("The string is empty\n");
    else
        printf("The string is not empty\n");
    return 0;
}
The string is empty
The string is not empty
No output
0
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The function printf() returns the number of charecters printed on the console.

Step 1: char a[] = "\0"; The variable a is declared as an array of characters and it initialized with "\0". It denotes that the string is empty.

Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero). Hence the if condition is failed.

In the else part it prints "The string is not empty".


10.
What will be the output of the following program in 16 bit platform assuming that 1022 is memory address of the string "Hello1" (in Turbo C under DOS) ?
#include<stdio.h>

int main()
{
    printf("%u %s\n", &"Hello1", &"Hello2");
    return 0;
}
1022 Hello2
Hello1 1022
Hello1 Hello2
1022 1022
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

In printf("%u %s\n", &"Hello", &"Hello");.

The %u format specifier tells the compiler to print the memory address of the "Hello1".

The %s format specifier tells the compiler to print the string "Hello2".

Hence the output of the program is "1022 Hello2".


11.
If a char is 1 byte wide, an integer is 2 bytes wide and a long integer is 4 bytes wide then will the following structure always occupy 7 bytes?
struct ex
{
    char ch;
    int i;
    long int a;
};
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
A compiler may leave holes in structures by padding the first char in the structure with another byte just to ensures that the integer that follows is stored at an location. Also, there might be 2extra bytes after the integer to ensure that the long integer is stored at an address, which is multiple of 4. Such alignment is done by machines to improve the efficiency of accessing values.

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

int main()
{
    unsigned char ch;
    FILE *fp;
    fp=fopen("trial", "r");
    while((ch = getc(fp))!=EOF)
        printf("%c", ch);
    fclose(fp);
    return 0;
}
Error: in unsigned char declaration
Error: while statement
No error
It prints all characters in file "trial"
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Here, EOF is -1. As 'ch' is declared as unsigned char it cannot deal with any negative value.

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

int main(int argc)
{
    printf("%d ", argc);
    fun(argc);
    return 0;
}
void fun(int i)
{
    if(i!=4)
        main(++i);
}
1 2 3
1 2 3 4
2 3 4
1
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    unsigned int num;
    int i;
    scanf("%u", &num);
    for(i=0; i<16; i++)
    {
        printf("%d", (num<<i & 1<<15)?1:0);
    }
    return 0;
}
It prints all even bits from num
It prints all odd bits from num
It prints binary equivalent num
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

If we give input 4, it will print 00000000 00000100 ;

If we give input 3, it will print 00000000 00000011 ;

If we give input 511, it will print 00000001 11111111 ;


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

int main()
{
    int i=10;
    const int *ptr = &i;
    fun(&ptr);
    return 0;
}
int fun(int **ptr)
{
    int j = 223;
    int *temp = &j;
    printf("Before changing ptr = %5x\n", *ptr);
    const *ptr = temp;
    printf("After changing ptr = %5x\n", *ptr);
    return 0;
}
Address of i
Address of j
10
223
Error: cannot convert parameter 1 from 'const int **' to 'int **'
Garbage value
Your Answer: Option
(Not Answered)
Correct Answer: Option

16.
Can I increase the size of dynamically allocated array?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Use realloc(variable_name, value);

17.
It is necessary to call the macro va_end if va_start is called in the function.
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
What do the following declaration signify?
void (*cmp)();
cmp is a pointer to an void function type.
cmp is a void type pointer function.
cmp is a function that return a void pointer.
cmp is a pointer to a function which returns void .
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    char huge *near *far *ptr1;
    char near *far *huge *ptr2;
    char far *huge *near *ptr3;
    printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
    return 0;
}
4, 4, 8
2, 4, 4
4, 4, 2
2, 4, 8
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    char *i = "55.555";
    int result1 = 10;
    float result2 = 11.111;
    result1 = result1+atoi(i);
    result2 = result2+atof(i);
    printf("%d, %f", result1, result2);
    return 0;
}
55, 55.555
66, 66.666600
65, 66.666000
55, 55
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Function atoi() converts the string to integer.
Function atof() converts the string to float.

result1 = result1+atoi(i);
Here result1 = 10 + atoi(55.555);
result1 = 10 + 55;
result1 = 65;

result2 = result2+atof(i);
Here result2 = 11.111 + atof(55.555);
result2 = 11.111 + 55.555000;
result2 = 66.666000;
So the output is "65, 66.666000" .


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