Sample questions
Question 1Medium
Which of the following declaration is correct?
int length;
char int;
int long;
float double;
Answer: A.
Explanation
int length;
denotes that variable length is int(integer) data type.
char int;
here int is a keyword cannot be used a variable name.
int long;
here long is a keyword cannot be used a variable name.
float double;
here double is a keyword cannot be used a variable name. So, the answer is int length;(Option A).
Step-by-step solution
- Int length; declares a variable of type int named length, and length is a valid name.
- Char int; uses int as a variable name, but int is a reserved keyword.
- Int long; uses long as a name, which is reserved, and two type words together are invalid.
- Float double; uses double as a name, which is reserved.
- The answer is int length;.
Why the other options are wrong
- B. Int is a reserved keyword and cannot be used as a variable name.
- C. Long is a reserved keyword and two type words together are invalid.
- D. Double is a reserved keyword and cannot be used as a variable name.
Tip: Reserved keywords such as int, long and double cannot be used as names.
Question 2Medium
Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1?
rem = 3.14 % 2.1;
rem = modf(3.14, 2.1);
rem = fmod(3.14, 2.1);
Remainder cannot be obtain in floating point dividation
Answer: C.
Explanation
fmod(x,y)
- Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator.
But
fmod()
performs floating point divisions.
Example:
#include <stdio.h>
#include <math.h>
int main ()
{
printf ("fmod of 3.14/2.1 is %lf\n", fmod (3.14,2.1) );
return 0;
}
Output:
fmod of 3.14/2.1 is 1.040000
Step-by-step solution
- The
% operator needs integer operands in C, so it cannot be used with 3.14 and 2.1. Modf splits one value into integer and fraction parts; it does not give the remainder of two values.fmod(x, y) gives the floating-point remainder of x divided by y.- So the correct statement is
```c
rem = fmod(3.14, 2.1);
```
, which is option C.
Why the other options are wrong
- A. The % operator needs integer operands, so this does not compile.
- B. Modf takes one value and a pointer, so it does not give a remainder of two values.
- D. A remainder can be found in floating point using fmod.
Tip: Use fmod for floating-point remainders.
Question 3Medium
Which of the following is not user defined data type...?
1 :struct book
{
char name[10];
float price;
int pages;
};
2 : long int l = 2.35;
3 : enum day {Sun, Mon, Tue, Wed};
- 1
- 2
- 3
- Both 1 and 2
Answer: B. 2
Explanation
C data types classification are
Primary data types
int
char
float
double
void
Secondary data types (or) User-defined data type
Array
Pointer
Structure
Union
Enum
So, clearly
long int l = 2.35;
is not User-defined data type.
(i.e.long int l = 2.35; is the answer.)
Step-by-step solution
- Item 1 is a struct declaration, so struct book is a user-defined type.
- Item 3 is an enum declaration, so enum day is also a user-defined type.
- Item 2 is a variable declaration, long int l = 2.35, which uses the built-in type long int.
- Item 2 defines no new type, so it is not a user-defined type.
- So the answer is 2.
Why the other options are wrong
- A. Item 1 is a struct, which is user-defined.
- C. Item 3 is an enum, which is user-defined.
- D. Item 1 is user-defined, so both 1 and 2 is wrong.
Tip: User-defined types are created with struct, union, enum or typedef.
Question 4Medium
By default a real number is treated as a?
- float
- double
- long double
- far double
Answer: B. double
Explanation
In computing, 'real number' often refers to non-complex floating-point numbers.
It include both rational numbers, such as 42 and 3/4, and irrational numbers such as pi = 3.14159265...
When the accuracy of the floating point number is insufficient, we can use thedouble to define the number.
The double is same as float but with longer precision and takes double space (8 bytes) than float.
To extend the precision further we can use long double which occupies 10 bytes of memory space.
Step-by-step solution
- A real number literal such as 3.14 has type double by default in C.
- Float needs an f suffix and long double needs an l suffix, so neither is the default.
- So the default is double, which is option B: double.
Why the other options are wrong
- A. Float is used only with an f suffix or when declared as float.
- C. Long double needs an l suffix, so it is not the default.
- D. Far double is not a C type.
Tip: Plain decimal literals in C are double.
Question 5Medium
How would you round off a value from 1.66 to 2.0?
ceil(1.66)
floor(1.66)
roundup(1.66)
roundto(1.66)
Answer: A.
Explanation
The C library function
double ceil(double x)
returns the smallest integer value greater than or equal to x.
Syntax:
Declaration: double ceil(double);
/* Example for ceil() and floor() functions: */
#include<stdio.h>
#include<math.h>
int main()
{
printf("\n Result : %f" , ceil(1.44) );
printf("\n Result : %f" , ceil(1.66) );
printf("\n Result : %f" , floor(1.44) );
printf("\n Result : %f" , floor(1.66) );
return 0;
}
// Output:
// Result : 2.000000
// Result : 2.000000
// Result : 1.000000
// Result : 1.000000
Step-by-step solution
- To round off a value from 1.66 to 2.0, we need the smallest integer greater than or equal to 1.66.
- The C library function
ceil(1.66) returns this value. - Therefore,
ceil(1.66) equals 2.0.
Why the other options are wrong
- B. It rounds down instead of up.
- C. It's not a standard function name in C.
- D. It's not a valid function in C.
Question 6Medium
What is the output of this C code?
#include <stdio.h>
void foo(const int *);
int main()
{
const int i = 10;
printf("%d ", i);
foo(&i);
printf("%d", i);
}
void foo(const int *i)
{
*i = 20;
}
- Compile time error
- 10 20
- Undefined value
- 10
Answer: A. Compile time error
Explanation
Cannot change a const type value.
Output:
$ cc pgm1.c
pgm1.c: In function foo:
pgm1.c:13: error: assignment of read-only location *i
Step-by-step solution
- I is declared const int, so its value must not be changed.
- Foo takes a const int * and does *i = 20, which assigns to a const object through a pointer.
- The compiler rejects this assignment as a read-only location before the program runs.
- So the program does not compile.
- The answer is Compile time error.
Why the other options are wrong
- B. The program never runs because the assignment in foo is rejected at compile time.
- C. There is no undefined value; the code is rejected at compile time.
- D. The first printf would show 10, but the code does not compile, so nothing is run.
Tip: Assigning through a pointer to const is a compile error in C.
Question 7Medium
Which of the following special symbol allowed in a variable name?
- * (asterisk)
- | (pipeline)
- - (hyphen)
- _ (underscore)
Answer: D. _ (underscore)
Explanation
Variable names in C are made up of letters (upper and lower case) and digits.
The underscore character ("_") is also permitted. Names must not begin with a digit
Examples of valid (but not very descriptive) C variable names:
=> foo
=> Bar
=> BAZ
=> foo_bar
=> _foo42
=> _
=> QuUx
Step-by-step solution
- Variable names in C can include letters, digits, and underscores.
- The underscore character ('_') is explicitly allowed in variable names.
- None of the other options (*, |, -) are valid in C variable names.
Why the other options are wrong
- A. (asterisk) is not allowed.
- B. | (pipeline) is not allowed.
- C. (hyphen) is not allowed.
Tip: Remember: In C, use letters, digits, and underscores for variable names; avoid special characters like *, |, or -.
Question 8Medium
Which of the following operations are INCORRECT...?
int i = 35; i = i%5;
short int j = 255; j = j;
long int k = 365L; k = k;
float a = 3.14; a = a%3;
Answer: D.
Explanation
float a = 3.14; a = a%3;
gives "Illegal use of floating point" error.
The modulus (%) operator can only be used on integer types.
We have to use
fmod()
function in math.h for float values.
Step-by-step solution
- The
% operator needs integer operands in C. - Options A, B and C use
% or assignments with integer types, which are valid. - In option D,
a is a float, so a%3 is an error. - So the incorrect operation is
```c
float a = 3.14;
a = a%3;
```
Why the other options are wrong
- A. I%5 is valid for an int, so this operation is correct.
- B. Assigning a short int to itself is valid.
- C. Assigning a long int to itself is valid.
Tip: The % operator works only on integer types.
Question 9Medium
Identify which of the following are declarations
1:extern int x;
2:float square ( float x ) { ... }
3:double pow(double, double);
- 1
- 2
- 1 and 3
- 3
Answer: C. 1 and 3
Explanation
extern int x;
Is an external variable declaration.
double pow(double, double);
Is a function prototype declaration. Therefore, 1 and 3 are declarations. 2 is definition.
Step-by-step solution
- Statement 1,
```c
extern int x;
```
, declares
x without creating it, so it is a declaration. - Statement 2,
```c
float square(float x) { ... }
```
, has a body, so it is a function definition.
- Statement 3,
```c
double pow(double, double);
```
, is a function prototype, so it is a declaration.
- Statements 1 and 3 are declarations, so the answer is 1 and 3.
Why the other options are wrong
- A. Statement 3 is also a declaration, a function prototype.
- B. Statement 2 has a body, so it is a definition and not a declaration.
- D. Statement 1 is also a declaration, so only statement 3 is not enough.
Tip: A declaration has no body; a definition has a body or allocates storage.
Question 10Medium
Is the following statement a declaration or definition?
extern int i;
- Declaration
- Definition
- Function
- Error
Answer: A. Declaration
Explanation
Declaring is the way a programmer tells the compiler to expect a particular type, be it a variable, class/struct/union type, a function type (prototype) or a particular object instance.
(ie. extern int i;)
Declaration never reserves any space for the variable or instance in the program's memory;
It simply a "hint" to the compiler that a use of the variable or instance is expected in the program. This hinting is technically called "forward reference".
Step-by-step solution
- Extern int i; tells the compiler that i exists and is defined in another file.
- It reserves no storage and gives no initial value, so it is not a definition.
- A declaration with extern and no initialiser is a declaration.
- So the answer is Declaration.
Why the other options are wrong
- B. A definition would allocate storage, which extern does not do here.
- C. The statement declares a variable, not a function.
- D. The statement is valid C, so there is no error.
Tip: A declaration with extern does not create storage, so it is not a definition.
Practise all 13 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →