Basics Questions with Answers

13 practice questions on Basics (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 13 free in the AptiNest app.

Sample questions

Question 1Medium

Which of the following declaration is correct?

  1. int length;
  2. char int;
  3. int long;
  4. 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

  1. Int length; declares a variable of type int named length, and length is a valid name.
  2. Char int; uses int as a variable name, but int is a reserved keyword.
  3. Int long; uses long as a name, which is reserved, and two type words together are invalid.
  4. Float double; uses double as a name, which is reserved.
  5. 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?

  1. rem = 3.14 % 2.1;
  2. rem = modf(3.14, 2.1);
  3. rem = fmod(3.14, 2.1);
  4. 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

  1. The % operator needs integer operands in C, so it cannot be used with 3.14 and 2.1.
  2. Modf splits one value into integer and fraction parts; it does not give the remainder of two values.
  3. fmod(x, y) gives the floating-point remainder of x divided by y.
  4. 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. 1
  2. 2
  3. 3
  4. 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

  1. Item 1 is a struct declaration, so struct book is a user-defined type.
  2. Item 3 is an enum declaration, so enum day is also a user-defined type.
  3. Item 2 is a variable declaration, long int l = 2.35, which uses the built-in type long int.
  4. Item 2 defines no new type, so it is not a user-defined type.
  5. 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?

  1. float
  2. double
  3. long double
  4. 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

  1. A real number literal such as 3.14 has type double by default in C.
  2. Float needs an f suffix and long double needs an l suffix, so neither is the default.
  3. 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?

  1. ceil(1.66)
  2. floor(1.66)
  3. roundup(1.66)
  4. 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

  1. To round off a value from 1.66 to 2.0, we need the smallest integer greater than or equal to 1.66.
  2. The C library function ceil(1.66) returns this value.
  3. 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;
    }
  1. Compile time error
  2. 10 20
  3. Undefined value
  4. 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

  1. I is declared const int, so its value must not be changed.
  2. Foo takes a const int * and does *i = 20, which assigns to a const object through a pointer.
  3. The compiler rejects this assignment as a read-only location before the program runs.
  4. So the program does not compile.
  5. 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?

  1. * (asterisk)
  2. | (pipeline)
  3. - (hyphen)
  4. _ (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

  1. Variable names in C can include letters, digits, and underscores.
  2. The underscore character ('_') is explicitly allowed in variable names.
  3. 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...?

  1. int i = 35; i = i%5;
  2. short int j = 255; j = j;
  3. long int k = 365L; k = k;
  4. 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

  1. The % operator needs integer operands in C.
  2. Options A, B and C use % or assignments with integer types, which are valid.
  3. In option D, a is a float, so a%3 is an error.
  4. 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. 1
  2. 2
  3. 1 and 3
  4. 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

  1. Statement 1, ```c extern int x; ``` , declares x without creating it, so it is a declaration.
  2. Statement 2, ```c float square(float x) { ... } ``` , has a body, so it is a function definition.
  3. Statement 3, ```c double pow(double, double); ``` , is a function prototype, so it is a declaration.
  4. 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;
  1. Declaration
  2. Definition
  3. Function
  4. 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

  1. Extern int i; tells the compiler that i exists and is defined in another file.
  2. It reserves no storage and gives no initial value, so it is not a definition.
  3. A declaration with extern and no initialiser is a declaration.
  4. 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 →

Related topics

See all C topics · All topics