Online C Programming Test - Engineering Mechanics 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: 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 is correct about err used in the declaration given below?
 typedef enum error { warning, test, exception } err;
It is a typedef for enum error.
It is a variable of type enum error.
The statement is erroneous.
It is a structure.
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

A typedef gives a new name to an existing data type.
So err is a new name for enum error.


2.
Which of the following correctly represents a long double constant?
6.68
6.68L
6.68f
6.68LF
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

6.68 is double.
6.68L is long double constant.
6.68f is float constant.
6.68LF is not allowed in c.


3.
Is it true that a global variable may have several declarations, but only one definition?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Yes, In all the global variable declarations, you need to use the keyword extern.


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


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

6.
Which of the following sentences are correct about a for loop in a C program?
1: for loop works faster than a while loop.
2: All things that can be done using a for loop can also be done using a while loop.
3: for(;;); implements an infinite loop.
4: for loop can be used if we want statements in a loop get executed at least once.
1
1, 2
2, 3
2, 3, 4
Your Answer: Option
(Not Answered)
Correct Answer: Option

7.
What will be the output of the program?
#include<stdio.h>
int main()
{
    float a=0.7;
    if(a < 0.7)
        printf("C\n");
    else
        printf("C++\n");
    return 0;
}
C
C++
Compiler error
Non of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

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

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

Output:
0.7000000000 0.6999999881


8.
What will be the output of the program?
#include<stdio.h>
int func1(int);

int main()
{
    int k=35;
    k = func1(k=func1(k=func1(k)));
    printf("k=%d\n", k);
    return 0;
}
int func1(int k)
{
    k++;
    return k;
}
k=35
k=36
k=37
k=38
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int k=35; The variable k is declared as an integer type and initialized to 35.

Step 2: k = func1(k=func1(k=func1(k))); The func1(k) increement the value of k by 1 and return it. Here the func1(k) is called 3 times. Hence it increements value of k = 35 to 38. The result is stored in the variable k = 38.

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


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

int main()
{
    while(i)
    {
        fun();
        main();
    }
    printf("Hello\n");
    return 0;
}
int fun()
{
    printf("Hi");
}
Hello
Hi Hello
No output
Infinite loop
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

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

Step 1: int fun(); This prototype tells the compiler that the function fun() does not accept any arguments and it returns an integer value.

Step 1: while(i) The value of i is not initialized so this while condition is failed. So, it does not execute the while block.

Step 1: printf("Hello\n"); It prints "Hello".

Hence the output of the program is "Hello".


10.
In C all functions except main() can be called recursively.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Any function including main() can be called recursively.

11.
Functions can be called either by value or reference
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

True, A function can be called either call by value or call by reference.

Example:

Call by value means c = sub(a, b); here value of a and b are passed.

Call by reference means c = sub(&a, &b); here address of a and b are passed.


12.
If a function contains two return statements successively, the compiler will generate warnings. Yes/No ?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Yes. If a function contains two return statements successively, the compiler will generate "Unreachable code" warnings.

Example:


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

int main()
{
    int a = 4, b = 3, c;
    c = mul(a, b);
    printf("c = %d\n", c);
    return 0;
}
int mul(int a, int b)
{
   return (a * b);
   return (a - b); /* Warning: Unreachable code */
}

Output:
c = 12


13.
What will be the output of the program?
#include<stdio.h>
#define MESS junk

int main()
{
    printf("MESS\n");
    return 0;
}
junk
MESS
Error
Nothing will print
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

printf("MESS\n"); It prints the text "MESS". There is no macro calling inside the printf statement occured.


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


15.
Preprocessor directive #ifdef .. #else ... #endif is used for conditional compilation.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

True, these macros are used for conditional operation.


#if <constant-expression>
#elif <constant-expression>
#endif

16.
Will the program compile successfully?
#include<stdio.h>
#define X (4+Y)
#define Y (X+3)

int main()
{
    printf("%d\n", 4*X+2);
    return 0;
}
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Reports an error: Undefined symbol 'X'

17.
What will be the output of the program assuming that the array begins at location 1002?
#include<stdio.h>

int main()
{
    int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2}, 
                       {2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} };
    printf("%u, %u, %u, %d\n", a, *a, **a, ***a);
    return 0;
}
1002, 2004, 4008, 2
2004, 4008, 8016, 1
1002, 1002, 1002, 1
Error
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    void *vp;
    char ch=74, *cp="JACK";
    int j=65;
    vp=&ch;
    printf("%c", *(char*)vp);
    vp=&j;
    printf("%c", *(int*)vp);
    vp=cp;
    printf("%s", (char*)vp+2);
    return 0;
}
JCK
J65K
JAK
JACK
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    int i, n;
    char *x="Alice";
    n = strlen(x);
    *x = x[n];
    for(i=0; i<=n; i++)
    {
        printf("%s ", x);
        x++;
    }
    printf("\n", x);
    return 0;
}
Alice
ecilA
Alice lice ice ce e
lice ice ce e
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

If you compile and execute this program in windows platform with Turbo C, it will give "lice ice ce e".

It may give different output in other platforms (depends upon compiler and machine). The online C compiler given in this site will give the Option C as output (it runs on Linux platform).


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

int main()
{
    int arr[3][3] = {1, 2, 3, 4};
    printf("%d\n", *(*(*(arr))));
    return 0;
}
Output: Garbage value
Output: 1
Output: 3
Error: Invalid indirection
Your Answer: Option
(Not Answered)
Correct Answer: Option

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