Online C Programming Test - C Programming Test 1

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 statements are correct about the below C-program?
#include<stdio.h>
int main()
{
    int x = 10, y = 100%90, i;
    for(i=1; i<10; i++)
    if(x != y);
        printf("x = %d y = %d\n", x, y);
    return 0;
}
1 : The printf() function is called 10 times.
2 : The program will produce the output x = 10 y = 10
3 : The ; after the if(x!=y) will NOT produce an error.
4 : The program will not produce output.
1
2, 3
3, 4
4
Your Answer: Option
(Not Answered)
Correct Answer: Option

2.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int k, num=30;
    k = (num>5 ? (num <=10 ? 100 : 200): 500);
    printf("%d\n", num);
    return 0;
}
200
30
100
500
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int k, num=30; here variable k and num are declared as an integer type and variable num is initialized to '30'.
Step 2: k = (num>5 ? (num <=10 ? 100 : 200): 500); This statement does not affect the output of the program. Because we are going to print the variable num in the next statement. So, we skip this statement.
Step 3: printf("%d\n", num); It prints the value of variable num '30'
Step 3: Hence the output of the program is '30'


3.
Point out the error in the following program.
#include<stdio.h>
int main()
{
    struct emp
    {
        char name[20];
        float sal;
    };
    struct emp e[10];
    int i;
    for(i=0; i<=9; i++)
        scanf("%s %f", e[i].name, &e[i].sal);
    return 0;
}
Suspicious pointer conversion
Floating point formats not linked (Run time error)
Cannot use scanf() for structures
Strings cannot be nested inside structures
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Compile and Run the above program in Turbo C:

C:\>myprogram.exe
Sundar
2555.50
scanf : floating point formats not linked
Abnormal program termination

The program terminates abnormally at the time of entering the float value for e[i].sal.

Solution:

Just add the following function in your program. It will force the compiler to include required libraries for handling floating point linkages.

static void force_fpf() /* A dummy function */
{
    float x, *y; /* Just declares two variables */
    y = &x;      /* Forces linkage of FP formats */
    x = *y;      /* Suppress warning message about x */
}


4.
The keyword used to transfer control from a function back to the calling function is
switch
goto
go back
return
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The keyword return is used to transfer control from a function back to the calling function.

Example:


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

int main()
{
    int a = 4, b = 3, c;
    c = add(a, b);
    printf("c = %d\n", c);
    return 0;
}
int add(int a, int b)
{
/* returns the value and control back to main() function */
   return (a+b);
}

Output:
c = 7


5.
Which of the following statements are correct about the function?
long fun(int num)
{
    int i;
    long f=1;
    for(i=1; i<=num; i++)
        f = f * i;
    return f;
}
The function calculates the value of 1 raised to power num.
The function calculates the square root of an integer
The function calculates the factorial value of an integer
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Yes, this function calculates and return the factorial value of an given integer num.


6.
Which of the statements is correct about the program?
#include<stdio.h>

int main()
{
    float a=3.14;
    char *j;
    j = (char*)&a;
    printf("%d\n", *j);
    return 0;
}
It prints ASCII value of the binary number present in the first byte of a float variable a.
It prints character equivalent of the binary number present in the first byte of a float variable a.
It will print 3
It will print a garbage value
Your Answer: Option
(Not Answered)
Correct Answer: Option

7.
Will the program compile in Turbo C?
#include<stdio.h>
int main()
{
    int a=10, *j;
    void *k;
    j=k=&a;
    j++;
    k++;
    printf("%u %u\n", j, k);
    return 0;
}
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Error in statement k++. We cannot perform arithmetic on void pointers.

The following error will be displayed while compiling above program in TurboC.

Compiling PROGRAM.C:
Error PROGRAM.C 8: Size of the type is unknown or zero.


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


9.
The library function used to find the last occurrence of a character in a string is
strnstr()
laststr()
strrchr()
strstr()
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Declaration: char *strrchr(const char *s, int c);

It scans a string s in the reverse direction, looking for a specific character c.

Example:

#include <string.h>
#include <stdio.h>

int main(void)
{
   char text[] = "I learn through IndiaBIX.com";
   char *ptr, c = 'i';

   ptr = strrchr(text, c);
   if (ptr)
      printf("The position of '%c' is: %d\n", c, ptr-text);
   else
      printf("The character was not found\n");
   return 0;
}

Output:

The position of 'i' is: 19


10.
If char=1, int=4, and float=4 bytes size, What will be the output of the program ?
#include<stdio.h>

int main()
{
    char ch = 'A';
    printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
    return 0;
}
1, 2, 4
1, 4, 4
2, 2, 4
2, 4, 8
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: char ch = 'A'; The variable ch is declared as an character type and initialized with value 'A'.

Step 2:

printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14));

The sizeof function returns the size of the given expression.

sizeof(ch) becomes sizeof(char). The size of char is 1 byte.

sizeof('A') becomes sizeof(65). The size of int is 4 bytes (as mentioned in the question).

sizeof(3.14f). The size of float is 4 bytes.

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


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

int main()
{
    char str[] = "Nagpur";
    str[0]='K';
    printf("%s, ", str);
    str = "Kanpur";
    printf("%s", str+1);
    return 0;
}
Kagpur, Kanpur
Nagpur, Kanpur
Kagpur, anpur
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The statement str = "Kanpur"; generates the LVALUE required error. We have to use strcpy function to copy a string.

To remove error we have to change this statement str = "Kanpur"; to strcpy(str, "Kanpur");

The program prints the string "anpur"


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

int main()
{
    int i=4, j=8;
    printf("%d, %d, %d\n", i|j&j|i, i|j&j|i, i^j);
    return 0;
}
12, 12, 12
112, 1, 12
32, 1, 12
-64, 1, 12
Your Answer: Option
(Not Answered)
Correct Answer: Option

13.
Bit fields CANNOT be used in union.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The following is the example program to explain "using bit fields inside an union".

#include<stdio.h>

union Point
{
  unsigned int x:4;
  unsigned int y:4;
  int res;
};

int main()
{
   union  Point pt;

   pt.x = 2;
   pt.y = 3;
   pt.res = pt.y;

   printf("\n The value of res = %d" , pt.res);

   return 0;
}
// Output: The value of res = 3


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

int main()
{
    FILE *fp;
    char ch;
    int i=1;
    fp = fopen("myfile.c", "r");
    while((ch=getc(fp))!=EOF)
    {
        if(ch == '\n')
            i++;
    }
    fclose(fp);
    return 0;
}
The code counts number of characters in the file
The code counts number of words in the file
The code counts number of blank lines in the file
The code counts number of lines in the file
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

This program counts the number of lines in the file myfile.c by counting the character '\n' in that file.


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

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

int main()
{
    char c=48;
    int i, mask=01;
    for(i=1; i<=5; i++)
    {
        printf("%c", c|mask);
        mask = mask<<1;
    }
    return 0;
}
12400
12480
12500
12556
Your Answer: Option
(Not Answered)
Correct Answer: Option

17.
What is the output of the program?
typedef struct data;
{
    int x;
    sdata *b;
}sdata;
Error: Declaration missing ';'
Error: in typedef
No error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
since the type 'sdata' is not known at the point of declaring the structure

18.
Which header file should you include, if you are going to develop a function, which can accept variable number of arguments?
varagrg.h
stdlib.h
stdio.h
stdarg.h
Your Answer: Option
(Not Answered)
Correct Answer: Option

19.
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>
void varfun(int n, ...);

int main()
{
    varfun(3, 7, -11.2, 0.66);
    return 0;
}
void varfun(int n, ...)
{
    float *ptr;
    int num;
    va_start(ptr, n);
    num = va_arg(ptr, int);
    printf("%d", num);
}
Error: too many parameters
Error: invalid access to list member
Error: ptr must be type of va_list
No error
Your Answer: Option
(Not Answered)
Correct Answer: Option

20.
va_list is an array that holds information needed by va_arg and va_end
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

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