Online C Programming Test - C Programming Test 6

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


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


3.
Which of the following statements are correct about an if-else statements in a C-program?
1: Every if-else statement can be replaced by an equivalent statements using   ?: operators
2: Nested if-else statements are allowed.
3: Multiple statements in an if block are allowed.
4: Multiple statements in an else block are allowed.
1 and 2
2 and 3
1, 2 and 4
2, 3, 4
Your Answer: Option
(Not Answered)
Correct Answer: Option

4.
Can we use a switch statement to switch on strings?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
The cases in a switch must either have integer constants or constant expressions.

5.
What will be the output of the program?
#include<stdio.h>
#include<math.h>
int main()
{
    printf("%f\n", sqrt(36.0));
    return 0;
}
6.0
6
6.000000
Error: Prototype sqrt() not found.
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

printf("%f\n", sqrt(36.0)); It prints the square root of 36 in the float format(i.e 6.000000).

Declaration Syntax: double sqrt(double x) calculates and return the positive square root of the given number.


6.
Is it true that too many recursive calls may result into stack overflow?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Yes, too many recursive calls may result into stack overflow. because when a function is called its return address is stored in stack.

After sometime the stack memory will be filled completely. Hence stack overflow error will occur.


7.
A macro must always be defined in capital letters.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

FALSE, The macro is case insensitive.


8.
In a macro call the control is passed to the macro.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

False, Always the macro is substituted by the given text/expression.


9.
A header file contains macros, structure declaration and function prototypes.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

True, the header file contains classes, function prototypes, structure declaration, macros.


10.
Which of the following function sets first n characters of a string to a given character?
strinit()
strnset()
strset()
strcset()
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Declaration:

char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch

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

int main(void)
{
   char *string = "abcdefghijklmnopqrstuvwxyz";
   char letter = 'x';

   printf("string before strnset: %s\n", string);
   strnset(string, letter, 13);
   printf("string after  strnset: %s\n", string);

   return 0;
}

Output:

string before strnset: abcdefghijklmnopqrstuvwxyz

string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz


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


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

int main()
{
    char str1[] = "Hello";
    char str2[10];
    char *t, *s;
    s = str1;
    t = str2;
    while(*t=*s)
        *t++ = *s++;
    printf("%s\n", str2);
    return 0;
}
Hello
HelloHello
No output
ello
Your Answer: Option
(Not Answered)
Correct Answer: Option

13.
If the size of pointer is 4 bytes then What will be the output of the program ?
#include<stdio.h>

int main()
{
    char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"};
    printf("%d, %d", sizeof(str), strlen(str[0]));
    return 0;
}
22, 4
25, 5
24, 5
20, 2
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"}; The variable str is declared as an pointer to the array of 6 strings.

Step 2: printf("%d, %d", sizeof(str), strlen(str[0]));

sizeof(str) denotes 6 * 4 bytes = 24 bytes. Hence it prints '24'

strlen(str[0])); becomes strlen(Frogs)). Hence it prints '5';

Hence the output of the program is 24, 5

Hint: If you run the above code in 16 bit platform (Turbo C under DOS) the output will be 12, 5. Because the pointer occupies only 2 bytes. If you run the above code in Linux (32 bit platform), the output will be 24, 5 (because the size of pointer is 4 bytes).


14.
Point out the error in the program?
#include<stdio.h>

int main()
{
    FILE *fp;
    fp=fopen("trial", "r");
    fseek(fp, "20", SEEK_SET);
    fclose(fp);
    return 0;
}
Error: unrecognised Keyword SEEK_SET
Error: fseek() long offset value
No error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Instead of "20" use 20L since fseek() need a long offset value.

15.
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog friday tuesday sunday
/* myprog.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%c", *++argv[1]);
    return 0;
}
r
f
m
y
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

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

Step 1: const int x=5; The constant variable x is declared as an integer data type and initialized with value '5'.

Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer.

Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx.

Step 4: *ptrx = 10; Here we are indirectly trying to change the value of the constant vaiable x. This will result in an error.

To change the value of const variable x we have to use *(int *)&x = 10;


17.
Point out the error in the program (in Turbo-C).
#include<stdio.h>
#define MAX 128

int main()
{
    const int max=128;
    char array[max];
    char string[MAX];
    array[0] = string[0] = 'A';
    printf("%c %c\n", array[0], string[0]);
    return 0;
}
Error: unknown max in declaration/Constant expression required
Error: invalid array string
None of above
No error. It prints A A
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: A macro named MAX is defined with value 128

Step 2: const int max=128; The constant variable max is declared as an integer data type and it is initialized with value 128.

Step 3: char array[max]; This statement reports an error "constant expression required". Because, we cannot use variable to define the size of array.

To avoid this error, we have to declare the size of an array as static. Eg. char array[10]; or use macro char array[MAX];

Note: The above program will print A A as output in Unix platform.


18.
Point out the error in the program.
#include<stdio.h>
const char *fun();

int main()
{
    char *ptr = fun();
    return 0;
}
const char *fun()
{
    return "Hello";
}
Error: Lvalue required
Error: cannot convert 'const char *' to 'char *'.
No error and No output
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option

19.
Input/output function prototypes and macros are defined in which header file?
conio.h
stdlib.h
stdio.h
dos.h
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations.

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

int main()
{
    int i;
    char c;
    for(i=1; i<=5; i++)
    {
        scanf("%c", &c); /* given input is 'a' */
        printf("%c", c);
        ungetc(c, stdin);
    }
    return 0;
}
aaaa
aaaaa
Garbage value.
Error in ungetc statement.
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

for(i=1; i<=5; i++) Here the for loop runs 5 times.

Loop 1:
scanf("%c", &c); Here we give 'a' as input.
printf("%c", c); prints the character 'a' which is given in the previous "scanf()" statement.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

Loop 2:
Here the scanf("%c", &c); get the input from "stdin" because of "ungetc" function.
printf("%c", c); Now variable c = 'a'. So it prints the character 'a'.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

This above process will be repeated in Loop 3, Loop 4, Loop 5.


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