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 will be the output of the program?
#include<stdio.h>
int main()
{
    float a = 0.7;
    if(0.7 > a)
        printf("Hi\n");
    else
        printf("Hello\n");
    return 0;
}
Hi
Hello
Hi Hello
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

if(0.7 > a) here a is a float variable and 0.7 is a double constant. The double constant 0.7 is greater than the float variable a. Hence the if condition is satisfied and it prints 'Hi'
Example:

#include<stdio.h>
int main()
{
    float a=0.7;
    printf("%.10f %.10f\n",0.7, a);
    return 0;
}

Output:
0.7000000000 0.6999999881


2.
Which of the following are unary operators in C?
1. !
2. sizeof
3. ~
4. &&
1, 2
1, 3
2, 4
1, 2, 3
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

An operation with only one operand is called unary operation.
Unary operators:
! Logical NOT operator.
~ bitwise NOT operator.
sizeof Size-of operator.

&& Logical AND is a logical operator.

Therefore, 1, 2, 3 are unary operators.


3.
Two different operators would always have different Associativity.
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

No, Two different operators may have same associativity.

Example:
Arithmetic operators like ++, -- having Right-to-Left associativity.
Relational operators like >, >= also have Left-to-Right associativity.


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

int main()
{
    int i=0;
    i++;
    if(i<=5)
    {
        printf("IndiaBIX");
        exit(1);
        main();
    }
    return 0;
}
Prints "IndiaBIX" 5 times
Function main() doesn't calls itself
Infinite loop
Prints "IndiaBIx"
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int i=0; The variable i is declared as in integer type and initialized to '0'(zero).

Step 2: i++; Here variable i is increemented by 1. Hence i becomes '1'(one).

Step 3: if(i<=5) becomes if(1 <=5). Hence the if condition is satisfied and it enter into if block statements.

Step 4: printf("IndiaBIX"); It prints "IndiaBIX".

Step 5: exit(1); This exit statement terminates the program execution.

Hence the output is "IndiaBIx".


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


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

int main()
{
    int x=30, *y, *z;
    y=&x; /* Assume address of x is 500 and integer is 4 byte size */
    z=y;
    *y++=*z++;
    x++;
    printf("x=%d, y=%d, z=%d\n", x, y, z);
    return 0;
}
x=31, y=502, z=502
x=31, y=500, z=500
x=31, y=498, z=498
x=31, y=504, z=504
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    void fun(int, int[]);
    int arr[] = {1, 2, 3, 4};
    int i;
    fun(4, arr);
    for(i=0; i<4; i++)
        printf("%d,", arr[i]);
    return 0;
}
void fun(int n, int arr[])
{
    int *p=0;
    int i=0;
    while(i++ < n)
        p = &arr[i];
    *p=0;
}
2, 3, 4, 5
1, 2, 3, 4
0, 1, 2, 3
3, 2, 1 0
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts one integer value and one array as an arguments and does not return anything.

Step 2: int arr[] = {1, 2, 3, 4}; The variable a is declared as an integer array and it is initialized to

a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4

Step 3: int i; The variable i is declared as an integer type.

Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function.

Step 5: for(i=0; i<4; i++) { printf("%d,", arr[i]); } The for loop runs untill the variable i is less than '4' and it prints the each value of array a.

Hence the output of the program is 1,2,3,4


8.
What will be the output of the program given below in 16-bit platform ?
#include<stdio.h>

int main()
{
    enum value{VAL1=0, VAL2, VAL3, VAL4, VAL5} var;
    printf("%d\n", sizeof(var));
    return 0;
}
1
2
4
10
Your Answer: Option
(Not Answered)
Correct Answer: Option

9.
A pointer union CANNOT be created
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    int a=250;
    printf("%1d\n", a);
    return 0;
}
1250
2
50
250
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

int a=250; The variable a is declared as an integer type and initialized to value 250.

printf("%1d\n", a); It prints the value of variable a.

Hence the output of the program is 250.


11.
What will be the output of the program if value 25 given to scanf()?
#include<stdio.h>

int main()
{
    int i;
    printf("%d\n", scanf("%d", &i));
    return 0;
}
25
2
1
5
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The scanf function returns the number of input is given.

printf("%d\n", scanf("%d", &i)); The scanf function returns the value 1(one).

Therefore, the output of the program is '1'.


12.
Point out the correct statements about the program?
#include<stdio.h>

int main()
{
    FILE *fptr;
    char str[80];
    fptr = fopen("f1.dat", "w");
    if(fptr == NULL)
        printf("Cannot open file");
    else
    {
        while(strlen(gets(str))>0)
        {
            fputs(str, fptr);
            fputs("\n", fptr);
        }
        fclose(fptr);
    }
    return 0;
}
The code copies the content of one file to another
The code writes strings that are read from the keyboard into a file.
The code reads a file
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

This program get the input string from the user through gets function and store it in the file f1.txt using fputs function.


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.
If the following program (myproc.c) is present in the directory "C:\TC" then what will be output of the program if run it from DOS shell?
/* myproc.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%s", argv[0]);
    return 0;
}
SAMPLE.C
C:\TC\MYPROC.EXE
C:\TC
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
In order to execute it from DOS shell, we have to run the created EXE file by entering the exe file name as C:\TC>myproc <enter>.

15.
Which of the following is TRUE about argv?
It is an array of character pointers
It is a pointer to an array of character pointers
It is an array of strings
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option

16.
Bitwise | can be used to set multiple bits in number.
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

17.
Specify the 2 library functions to dynamically allocate memory?
malloc() and memalloc()
alloc() and memalloc()
malloc() and calloc()
memalloc() and faralloc()
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    union test
    {
        int i;
        float f;
        char c;
    };
    union test *t;
    t = (union test *)malloc(sizeof(union test));
    t->f = 10.10f;
    printf("%f", t->f);
    return 0;
}
10
Garbage value
10.100000
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option

19.
It is not necessary to typecast the address returned by malloc().
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

20.
The itoa function can convert an integer in decimal, octal or hexadecimal form to a string.
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

itoa() takes the integer input value input and converts it to a number in base radix. The resulting number a sequence of base-radix digits.

Example:


/* itoa() example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
	int no;
	char buff [50];
	printf ("Enter number: ");
	scanf ("%d",&no);
    
	itoa (no,buff,10);
	printf ("Decimal: %s\n",buff);
    
	itoa (no,buff,2);
	printf ("Binary: %s\n",buff);
    
	itoa (no,buff,16);
	printf ("Hexadecimal: %s\n",buff);
    
	return 0;
}

Output:
Enter a number: 1250
Decimal: 1250
Binary: 10011100010
Hexadecimal: 4e2


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