Online C Programming Test - Engineering Mechanics 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.
What will be the output of the program?
#include<stdio.h>
int X=40;
int main()
{
    int X=20;
    printf("%d\n", X);
    return 0;
}
20
40
Error
No Output
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Whenever there is conflict between a local variable and global variable, the local variable gets priority.

2.
What is the output of the program in Turbo C (in DOS 16-bit OS)?
#include<stdio.h>
int main()
{
    char *s1;
    char far *s2;
    char huge *s3;
    printf("%d, %d, %d\n", sizeof(s1), sizeof(s2), sizeof(s3));
    return 0;
}
2, 4, 6
4, 4, 2
2, 4, 4
2, 2, 2
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Any pointer size is 2 bytes. (only 16-bit offset)
So, char *s1 = 2 bytes.
So, char far *s2; = 4 bytes.
So, char huge *s3; = 4 bytes.
A far, huge pointer has two parts: a 16-bit segment value and a 16-bit offset value.

Since C is a compiler dependent language, it may give different output in other platforms. The above program works fine in Windows (TurboC), but error in Linux (GCC Compiler).


3.
1 : typedef long a;
extern int a c;
2 : typedef long a;
extern a int c;
3 : typedef long a;
extern a c;
1 correct
2 correct
3 correct
1, 2, 3 are correct
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

typedef long a;
extern int a c;
while compiling this statement becomes extern int long c;. This will result in to "Declaration syntax error".

typedef long a;
extern a int c;
while compiling this statement becomes extern long int c;. This will result in to "Too many types in declaration error".

typedef long a;
extern a c;
while compiling this statement becomes extern long c;. This is a valid c declaration statement. It says variable c is long data type and defined in some other file or module.

So, Option C is the correct answer.


4.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=4;
    switch(i)
    {
        default:
           printf("This is default\n");
        case 1:
           printf("This is case 1\n");
           break;
        case 2:
           printf("This is case 2\n");
           break;
        case 3:
           printf("This is case 3\n");
    }
    return 0;
}
This is default
This is case 1
This is case 3
This is default
This is case 1
This is case 3
This is default
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

In the very begining of switch-case statement default statement is encountered. So, it prints "This is default".

In default statement there is no break; statement is included. So it prints the case 1 statements. "This is case 1".

Then the break; statement is encountered. Hence the program exits from the switch-case block.


5.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        printf("Hello\n");
        case 1:
            printf("Hi\n");
            break;
        case 2:
            printf("\nBye\n");
            break;
    }
    return 0;
}
Hello
Hi
Hello
Bye
Hi
Bye
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

switch(i) has the variable i it has the value '1'(one).

Then case 1: statements got executed. so, it prints "Hi". The break; statement make the program to be exited from switch-case statement.

switch-case do not execute any statements outside these blocks case and default

Hence the output is "Hi".


6.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int x = 3;
    float y = 3.0;
    if(x == y)
        printf("x and y are equal");
    else
        printf("x and y are not equal");
    return 0;
}
x and y are equal
x and y are not equal
Unpredictable
No output
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int x = 3; here variable x is an integer type and initialized to '3'.
Step 2: float y = 3.0; here variable y is an float type and initialized to '3.0'
Step 3: if(x == y) here we are comparing if(3 == 3.0) hence this condition is satisfied.
Hence it prints "x and y are equal".


7.
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        case 1:
           printf("Case1");
           break;
        case 1*2+4:
           printf("Case2");
           break;
    }
return 0;
}
Error: in case 1*2+4 statement
Error: No default specified
Error: in switch statement
No Error
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Constant expression are accepted in switch

It prints "Case1"


8.
Which of the following statements are correct about the below program?
#include<stdio.h>
int main()
{
    int i = 0;
    i++;
    if(i <= 5)
    {
        printf("IndiaBIX\n");
        exit(0);
        main();
    }
    return 0;
}
The program prints 'IndiaBIX' 5 times
The program prints 'IndiaBIX' one time
The call to main() after exit() doesn't materialize.
The compiler reports an error since main() cannot call itself.
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int i = 0; here variable i is declared as an integer type and initialized to '0'(zero).
Step 2: i++; here variable i is increemented by 1(one). Hence, i = 1
Step 3: if(i <= 5) becomes if(1 <= 5) here we are checking '1' is less than or equal to '5'. Hence the if condition is satisfied.
Step 4: printf("IndiaBIX\n"); It prints "IndiaBIX"
Step 5: exit(); terminates the program execution.

Hence the output is "IndiaBIX".


9.
Will the expression *p = p be disallowed by the compiler?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Because, here even though the value of p is accessed twice it is used to modify two different objects p and *p

10.
If the binary eauivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000 0000 0000, what will be the output of the program (on intel machine)?
#include<stdio.h>
#include<math.h>
int main()
{
    float a=5.375;
    char *p;
    int i;
    p = (char*)&a;
    for(i=0; i<=3; i++)
        printf("%02x\n", (unsigned char)p[i]);
    return 0;
}
40 AC 00 00
04 CA 00 00
00 00 AC 40
00 00 CA 04
Your Answer: Option
(Not Answered)
Correct Answer: Option

11.
What will be the output of the program?
#include<stdio.h>
int fun(int, int);
typedef int (*pf) (int, int);
int proc(pf, int, int);

int main()
{
    printf("%d\n", proc(fun, 6, 6));
    return 0;
}
int fun(int a, int b)
{
   return (a==b);
}
int proc(pf p, int a, int b)
{
   return ((*p)(a, b));
}
6
1
0
-1
Your Answer: Option
(Not Answered)
Correct Answer: Option

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


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

int main()
{
    char *str;
    str = "%d\n";
    str++;
    str++;
    printf(str-2, 300);
    return 0;
}
No output
30
3
300
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    char str[20] = "Hello";
    char *const p=str;
    *p='M';
    printf("%s\n", str);
    return 0;
}
Mello
Hello
HMello
MHello
Your Answer: Option
(Not Answered)
Correct Answer: Option

15.
Which of the following statements correct about k used in the below statement?
char ****k;
k is a pointer to a pointer to a pointer to a char
k is a pointer to a pointer to a pointer to a pointer to a char
k is a pointer to a char pointer
k is a pointer to a pointer to a char
Your Answer: Option
(Not Answered)
Correct Answer: Option

16.
Are the expression *ptr++ and ++*ptr are same?
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
*ptr++ increments the pointer and not the value, whereas the ++*ptr increments the value being pointed by ptr

17.
Are the three declarations char **apple, char *apple[], and char apple[][] same?
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

18.
Is there any difference between the following two statements?
char *p=0;
char *t=NULL;
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
NULL is #defined as 0 in the 'stdio.h' file. Thus, both p and t are NULL pointers.

19.
Will the following program give any warning on compilation in TurboC (under DOS)?
#include<stdio.h>

int main()
{
    int *p1, i=25;
    void *p2;
    p1=&i;
    p2=&i;
    p1=p2;
    p2=p1;
    return 0;
}
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

20.
What will be the output of the program if the array begins 1200 in memory?
#include<stdio.h>

int main()
{
    int arr[]={2, 3, 4, 1, 6};
    printf("%u, %u, %u\n", arr, &arr[0], &arr);
    return 0;
}
1200, 1202, 1204
1200, 1200, 1200
1200, 1204, 1208
1200, 1202, 1200
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

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

Step 2: printf("%u, %u, %u\n", arr, &arr[0], &arr); Here,

The base address of the array is 1200.

=> arr, &arr is pointing to the base address of the array arr.

=> &arr[0] is pointing to the address of the first element array arr. (ie. base address)

Hence the output of the program is 1200, 1200, 1200


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