Online C Programming Test - C Programming Test 8

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.
Which of the following cannot be checked in a switch-case statement?
Character
Integer
Float
enum
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value.


switch( expression )
{
    case constant-expression1:    statements 1;
    case constant-expression2:    statements 2;    
    case constant-expression3:    statements3 ;
    ...
    ...
    default : statements 4;
}

The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.


2.
Point out the error, if any in the program.
#include<stdio.h> 
int main()
{
    int a = 10, b;
    a >=5 ? b=100: b=200;
    printf("%d\n", b);
    return 0;
}
100
200
Error: L value required for b
Garbage value
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Variable b is not assigned.

It should be like:

b = a >= 5 ? 100 : 200;


3.
Which of the following statements are correct about the program?
#include<stdio.h>
int main()
{
    int x = 30, y = 40;
    if(x == y)
        printf("x is equal to y\n");

    else if(x > y)
        printf("x is greater than y\n");

    else if(x < y)
        printf("x is less than y\n")
    return 0;
}
Error: Statement missing
Error: Expression syntax
Error: Lvalue required
Error: Rvalue required
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

This program will result in error "Statement missing ;"

printf("x is less than y\n") here ; should be added to the end of this statement.


4.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=4, j=-1, k=0, w, x, y, z;
    w = i || j || k;
    x = i && j && k;
    y = i || j &&k;
    z = i && j || k;
    printf("%d, %d, %d, %d\n", w, x, y, z);
    return 0;
}
1, 1, 1, 1
1, 1, 0, 1
1, 0, 0, 1
1, 0, 1, 1
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int i=4, j=-1, k=0, w, x, y, z; here variable i, j, k, w, x, y, z are declared as an integer type and the variable i, j, k are initialized to 4, -1, 0 respectively.

Step 2: w = i || j || k; becomes w = 4 || -1 || 0;. Hence it returns TRUE. So, w=1

Step 3: x = i && j && k; becomes x = 4 && -1 && 0; Hence it returns FALSE. So, x=0

Step 4: y = i || j &&k; becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y=1

Step 5: z = i && j || k; becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z=1.

Step 6: printf("%d, %d, %d, %d\n", w, x, y, z); Hence the output is "1, 0, 1, 1".


5.
Are the following two statement same?
1. a <= 20 ? (b = 30): (c = 30);
2. (a <=20) ? b : (c = 30);
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

No, the expressions 1 and 2 are not same.

1. a <= 20 ? (b = 30) : (c = 30); This statement can be rewritten as,


if(a <= 20)
{
    b = 30;
}
else
{
    c = 30;
}

2. (a <=20) ? b : (c = 30); This statement can be rewritten as,


if(a <= 20)
{
    //Nothing here
}
else
{
    c = 30;
}


6.
Macros have a local scope.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

False, The scope of macros is globals and functions. Also the scope of macros is only from the point of definition to the end of the file.


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

8.
Is this a correct way for NULL pointer assignment?
int i=0;
char *q=(char*)i;
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
The correct way is char *q=0 (or) char *q=(char*)0

9.
What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?
#include<stdio.h>

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
    printf("%u, %u\n", a+1, &a+1);
    return 0;
}
65474, 65476
65480, 65496
65480, 65488
65474, 65488
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a[3][4] is declared as an integer array having the 3 rows and 4 colums dimensions.

Step 2: printf("%u, %u\n", a+1, &a+1);

The base address(also the address of the first element) of array is 65472.

For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480

Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".

Hence, begining address 65472 + 24 = 65496. So, &a+1 = 65496

Hence the output of the program is 65480, 65496


10.
The library function used to reverse a string is
strstr()
strrev()
revstr()
strreverse()
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

strrev(s) Reverses all characters in s

Example:

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

int main(void)
{
   char *str = "IndiaBIX";

   printf("Before strrev(): %s\n", str);
   strrev(str);
   printf("After strrev():  %s\n", str);
   return 0;
}

Output:

Before strrev(): IndiaBIX

After strrev(): XIBaidnI


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

int main()
{
    char sentence[80];
    int i;
    printf("Enter a line of text\n");
    gets(sentence);
    for(i=strlen(sentence)-1; i >=0; i--)
        putchar(sentence[i]);
    return 0;
}
The sentence will get printed in same order as it entered
The sentence will get printed in reverse order
Half of the sentence will get printed
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

    struct course
    {
        int courseno;
        char coursename[25];
    };
int main()
{
    struct course c[] = { {102, "Java"}, 
                          {103, "PHP"}, 
                          {104, "DotNet"}     };

    printf("%d ", c[1].courseno);
    printf("%s\n", (*(c+2)).coursename);
    return 0;
}
103 DotNet
102 Java
103 PHP
104 DotNet
Your Answer: Option
(Not Answered)
Correct Answer: Option

13.
Point out the error in the program?
struct emp
{
    int ecode;
    struct emp *e;
};
Error: in structure declaration
Linker Error
No Error
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
This type of declaration is called as self-referential structure. Here *e is pointer to a struct emp.

14.
On declaring a structure 0 bytes are reserved in memory.
True
False
Your Answer: Option
(Not Answered)
Correct Answer: Option

15.
On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"?
#include<stdio.h>

int main()
{
    int i, fss;
    char ch, source[20] = "source.txt", target[20]="target.txt", t;
    FILE *fs, *ft;
    fs = fopen(source, "r");
    ft = fopen(target, "w");
    while(1)
    {
        ch=getc(fs);
        if(ch==EOF)
            break;
        else
        {
            fseek(fs, 4L, SEEK_CUR);
            fputc(ch, ft);
        }
    }
    return 0;
}
r n
Trh
err
None of above
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

The file source.txt is opened in read mode and target.txt is opened in write mode. The file source.txt contains "To err is human".

Inside the while loop,

ch=getc(fs); The first character('T') of the source.txt is stored in variable ch and it's checked for EOF.

if(ch==EOF) If EOF(End of file) is true, the loop breaks and program execution stops.

If not EOF encountered, fseek(fs, 4L, SEEK_CUR); the file pointer advances 4 character from the current position. Hence the file pointer is in 5th character of file source.txt.

fputc(ch, ft); It writes the character 'T' stored in variable ch to target.txt.

The while loop runs three times and it write the character 1st and 5th and 11th characters ("Trh") in the target.txt file.


16.
What will be the output of the program (sample.c) given below if it is executed from the command line (Turbo C in DOS)?
cmd> sample 1 2 3
/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    int j;
    j = argv[1] + argv[2] + argv[3];
    printf("%d", j);
    return 0;
}
6
sample 6
Error
Garbage value
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:

Here argv[1], argv[2] and argv[3] are string type. We have to convert the string to integer type before perform arithmetic operation.

Example: j = atoi(argv[1]) + atoi(argv[2]) + atoi(argv[3]);


17.
In Turbo C/C++ under DOS if we want that any wild card characters in the command-line arguments should be appropriately expanded, are we required to make any special provision?
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option
Explanation:
Yes you have to compile a program like
tcc myprog wildargs.obj

18.
Bitwise | can be used to multiply a number by powers of 2.
Yes
No
Your Answer: Option
(Not Answered)
Correct Answer: Option

19.
What do the following declaration signify?
int *f();
f is a pointer variable of function type.
f is a function returning pointer to an int.
f is a function pointer.
f is a simple declaration of pointer variable.
Your Answer: Option
(Not Answered)
Correct Answer: Option

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

int main()
{
    char huge *near *far *ptr1;
    char near *far *huge *ptr2;
    char far *huge *near *ptr3;
    printf("%d, %d, %d\n", sizeof(ptr1), sizeof(**ptr2), sizeof(ptr3));
    return 0;
}
4, 4, 4
4, 2, 2
2, 8, 4
2, 4, 8
Your Answer: Option
(Not Answered)
Correct Answer: Option

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