Online C Programming Test - C Programming Test 2

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 the while loop will get executed if a short int is 2 byte wide?
#include<stdio.h>
int main()
{
    int j=1;
    while(j <= 255)
    {
        printf("%c %d\n", j, j);
        j++;
    }
    return 0;
}
Infinite times
255 times
256 times
254 times
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The while(j <= 255) loop will get executed 255 times. The size short int(2 byte wide) does not affect the while() loop.


2.
Which of the following errors would be reported by the compiler on compiling the program given below?
#include<stdio.h>
int main()
{
    int a = 5;
    switch(a)
    {
	case 1:
	printf("First");

	case 2:
	printf("Second");

	case 3 + 2:
	printf("Third");

	case 5:
	printf("Final");
	break;

    }
    return 0;
}
There is no break statement in each case.
Expression as in case 3 + 2 is not allowed.
Duplicate case case 5:
No error will be reported.
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Because, case 3 + 2: and case 5: have the same constant value 5.


3.
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
    int P = 10;
    switch(P)
    {
       case 10:
       printf("Case 1");

       case 20:
       printf("Case 2");
       break;

       case P:
       printf("Case 2");
       break;
    }
    return 0;
}
Error: No default value is specified
Error: Constant expression required at line case P:
Error: There is no break statement in each case.
No error will be reported.
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The compiler will report the error "Constant expression required" in the line case P: . Because, variable names cannot be used with case statements.

The case statements will accept only constant expression.


4.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=2;
    printf("%d, %d\n", ++i, ++i);
    return 0;
}
3, 4
4, 3
4, 4
Output may vary from compiler to compiler
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The order of evaluation of arguments passed to a function call is unspecified.

Anyhow, we consider ++i, ++i are Right-to-Left associativity. The output of the program is 4, 3.

In TurboC, the output will be 4, 3.

In GCC, the output will be 4, 4.


5.
In the expression a=b=5 the order of Assignment is NOT decided by Associativity of operators
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The equal to = operator has Right-to-Left Associativity. So it assigns b=5 then a=b.


6.
What is the notation for following functions?
1.  int f(int a, float b)
    {
        /* Some code */
    }

2.  int f(a, b)
    int a; float b;
    {
        /* Some code */
    }
1. KR Notation
2. ANSI Notation
1. Pre ANSI C Notation
2. KR Notation
1. ANSI Notation
2. KR Notation
1. ANSI Notation
2. Pre ANSI Notation
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
KR Notation means Kernighan and Ritche Notation.

7.
How many times the program will print "IndiaBIX" ?
#include<stdio.h>

int main()
{
    printf("IndiaBIX");
    main();
    return 0;
}
Infinite times
32767 times
65535 times
Till stack overflows
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

A call stack or function stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing.

A stack overflow occurs when too much memory is used on the call stack.

Here function main() is called repeatedly and its return address is stored in the stack. After stack memory is full. It shows stack overflow error.


8.
What will be the output of the program?
#include<stdio.h>
#define SQR(x)(x*x)

int main()
{
    int a, b=3;
    a = SQR(b+2);
    printf("%d\n", a);
    return 0;
}
25
11
Error
Garbage value
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The macro function SQR(x)(x*x) calculate the square of the given number 'x'. (Eg: 102)

Step 1: int a, b=3; Here the variable a, b are declared as an integer type and the variable b is initialized to 3.

Step 2: a = SQR(b+2); becomes,

=> a = b+2 * b+2; Here SQR(x) is replaced by macro to x*x .

=> a = 3+2 * 3+2;

=> a = 3 + 6 + 2;

=> a = 11;

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

Hence the output of the program is 11


9.
A preprocessor directive is a message from programmer to the preprocessor.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

True, the programmer tells the compiler to include the preprocessor when compiling.


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

int main()
{
    char *p;
    p="hello";
    printf("%s\n", *&*&p);
    return 0;
}
llo
hello
ello
h
Your Answer: Option
(Not Answered)
Correct Answer: Option

11.
How will you free the allocated memory ?
remove(var-name);
free(var-name);
delete(var-name);
dalloc(var-name);
Your Answer: Option
(Not Answered)
Correct Answer: Option

12.
In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?
"I am a boy\r\n\0"
"I am a boy\r\0"
"I am a boy\n\0"
"I am a boy"
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Declaration: char *fgets(char *s, int n, FILE *stream);

fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first.

Therefore, the string str contain "I am a boy\n\0"


13.
Which files will get closed through the fclose() in the following program?
#include<stdio.h>

int main()
{
    FILE *fs, *ft, *fp;
    fp = fopen("A.C", "r");
    fs = fopen("B.C", "r");
    ft = fopen("C.C", "r");
    fclose(fp, fs, ft);
    return 0;
}
"A.C" "B.C" "C.C"
"B.C" "C.C"
"A.C"
Error in fclose()
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Extra parameter in call to fclose().


14.
If the file 'source.txt' contains a line "Be my friend" which of the following will be the output of below program?
#include<stdio.h>

int main()
{
    FILE *fs, *ft;
    char c[10];
    fs = fopen("source.txt", "r");
    c[0] = getc(fs);
    fseek(fs, 0, SEEK_END);
    fseek(fs, -3L, SEEK_CUR);
    fgets(c, 5, fs);
    puts(c);
    return 0;
}
friend
frien
end
Error in fseek();
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The file source.txt contains "Be my friend".

fseek(fs, 0, SEEK_END); moves the file pointer to the end of the file.

fseek(fs, -3L, SEEK_CUR); moves the file pointer backward by 3 characters.

fgets(c, 5, fs); read the file from the current position of the file pointer.

Hence, it contains the last 3 characters of "Be my friend".

Therefore, it prints "end".


15.
According to ANSI specifications which is the correct way of declaring main when it receives command-line arguments?
int main(int argc, char *argv[])
int main(argc, argv)
int argc; char *argv;
int main()
{
    int argc; char *argv;
}
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option

16.
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three
/* myprog.c */
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char **argv)
{
    printf("%s\n", *++argv);
    return 0;
}
myprog
one
two
three
Your Answer: Option
(Not Answered)
Correct Answer: Option

17.
Point out the correct statement which correctly allocates memory dynamically for 2D array following program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p, i, j;
    /* Add statement here */
    for(i=0; i<3; i++)
    {
        for(j=0; j<4; j++)
        {
            p[i*4+j] = i;
            printf("%d", p[i*4+j]);
        }
    }
    return 0;
}
p = (int*) malloc(3, 4);
p = (int*) malloc(3*sizeof(int));
p = malloc(3*4*sizeof(int));
p = (int*) malloc(3*4*sizeof(int));
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
malloc() allocates memory from the heap and not from the stack.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

19.
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>
void display(char *s, ...);
int fun1();
int fun2();

int main()
{
    int (*p1)();
    int (*p2)();
    p1 = fun1;
    p2 = fun2;
    display("IndiaBIX", p1, p2);
    return 0;
}
void display(char *s, ...)
{
    int (*pp1)();
    int (*pp2)();
    va_list ptr;

    va_start(ptr, s);
    pp1 = va_arg(ptr, int(*)());
    (*pp1)();

    pp2 = va_arg(ptr, int(*)());
    (*pp2)();

}
int fun1()
{
    printf("Hello");
}
int fun2()
{
    printf("Hi");
}
Error: invalid function display() call
Error: invalid va_start(ptr, s);
Error: va_arg cannot extract function pointer from variable argument list.
Error: Rvalue required for t
Your Answer: Option
(Not Answered)
Correct Answer: Option

20.
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 integer function pointer.
ptr is a array of 30 integer pointers.
ptr is a array 30 pointers.
Your Answer: Option
(Not Answered)
Correct Answer: Option

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