Sail E0 Webinar

MCQs

Total Questions : 16 | Page 1 of 2 pages
Question 1.


Point out the error in the program.


#include<stdio.h>
const char *fun();
int main()
{
*fun() = 'A';
return 0;
}
const char *fun()
{
return "Hello";
}
  1.    Error: RValue required
  2.    Error: Lvalue required
  3.    Error: fun() returns a pointer const character which cannot be modified
  4.    No error
 Discuss Question
Answer: Option C. -> Error: fun() returns a pointer const character which cannot be modified


Question 2.


Point out the error in the program.


#include<stdio.h>
int main()
{
const int k=7;
int *const q=&k;
printf("%d", *q);
return 0;
}
  1.    Error: RValue required
  2.    Error: Lvalue required
  3.    Error: cannot CONVERT from 'const int *' to 'int *const'
  4.    No error
 Discuss Question
Answer: Option D. -> No error

No error. This will produce 7 as output.


Question 3.


Point out the error in the program.


#include<stdio.h>
int main()
{
const int x;
x=128;
printf("%d\n", x);
return 0;
}
  1.    Error: unknown data type const int
  2.    Error: const variable have been initialised when declared.
  3.    Error: stack overflow in x
  4.    No error
 Discuss Question
Answer: Option B. -> Error: const variable have been initialised when declared.

A const variable has to be initialized when it is declared. later assigning the value to the const 

variable will result in an error "Cannot modify the const object".

Hence Option B is correct


Question 4.


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";
}
  1.    Error: Lvalue required
  2.    Error: cannot convert 'const char *' to 'char *'
  3.    No error and No output
  4.    None of above
 Discuss Question
Answer: Option C. -> No error and No output


Question 5.


Point out the error in the program.


#include<stdio.h>
#include<stdlib.h>
union employee
{
char name[15];
int age;
float salary;
};
const union employee e1;
int main()
{
strcpy(e1.name, "K");
printf("%s", e1.name);
e1.age=85;
printf("%d", e1.age);
printf("%f", e1.salary);
return 0;
}
  1.    Error: RValue required
  2.    Error: cannot modify const object
  3.    Error: LValue required in strcpy
  4.    No error
 Discuss Question
Answer: Option B. -> Error: cannot modify const object


Question 6.


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;
}
  1.    Error: unknown max in declaration/Constant expression required
  2.    Error: invalid array string
  3.    None of above
  4.    No error. It prints A A
 Discuss Question
Answer: Option A. -> Error: unknown max in declaration/Constant expression required

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.



Question 7.


What will be the output of the program?


#include<stdio.h>
int main()
{
const c = -11;
const int d = 34;
printf("%d, %d\n", c, d);
return 0;
}
  1.    Error
  2.    -11, 34
  3.    11, 34
  4.    None of these
 Discuss Question
Answer: Option B. -> -11, 34

Step 1: const c = -11; The constant variable 'c' is declared and initialized to value "-11".

Step 2: const int d = 34; The constant variable 'd' is declared as an integer and initialized to value '34'.

Step 3: printf("%d, %dn", c, d); The value of the variable 'c' and 'd' are printed.

Hence the output of the program is -11, 34


Question 8.


What will be the output of the program?


#include<stdio.h>
int main()
{
const int i=0;
printf("%d\n", i++);
return 0;
}
  1.    10
  2.    11
  3.    No output
  4.    Error: ++needs a value
 Discuss Question
Answer: Option D. -> Error: ++needs a value

This program will show an error "Cannot modify a const object".

Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value 

of '0'(zero).

Step 2: printf("%dn", i++); Here the variable 'i' is increemented by 1(one). This will create an error 

"Cannot modify a const object".

Because, we cannot modify a const variable.


Question 9.


What will be the output of the program (in Turbo C)?


#include<stdio.h>
int fun(int *f)
{
*f = 10;
return 0;
}
int main()
{
const int arr[5] = {1, 2, 3, 4, 5};
printf("Before modification arr[3] = %d", arr[3]);
fun(&arr[3]);
printf("\nAfter modification arr[3] = %d", arr[3]);
return 0;
}
A. Before modification arr[3] = 4
After modification arr[3] = 10
B. Error: cannot convert parameter 1 from const int * to int *
C. Error: Invalid parameter
D. Before modification arr[3] = 4
After modification arr[3] = 4
  1.    10
  2.    11
  3.    No output
  4.    Error: ++needs a value
 Discuss Question
Answer: Option D. -> Error: ++needs a value

Answer: Option A

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

arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5

Step 2: printf("Before modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 4).

Step 3: fun(&arr[3]); The memory location of the arr[3] is passed to fun() and arr[3]value is modified to 10.

A const variable can be indirectly modified by a pointer.

Step 4: printf("After modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 10).

Hence the output of the program is

Before modification arr[3] = 4

After modification arr[3] = 10



Question 10.


What will be the output of the program?


#include<stdio.h>
int get();
int main()
{
const int x = get();
printf("%d", x);
return 0;
}
int get()
{
return 20;
}
  1.    Garbage value
  2.    Error
  3.    20
  4.    0
 Discuss Question
Answer: Option C. -> 20

Step 1: int get(); This is the function prototype for the funtion get(), it tells the compiler returns 

an integer value and accept no parameters.

Step 2: const int x = get(); The constant variable x is declared as an integer data type and initialized 

with the value "20".

The function get() returns the value "20".

Step 3: printf("%d", x); It prints the value of the variable x.

Hence the output of the program is "20".


Latest Videos

Latest Test Papers