Interview Questions on Chapter 2: Fundamentals of C
Chapter 2: Fundamentals of C
1. What are tokens in C programming?
- Answer: Tokens are the smallest units of a C program. They include keywords, identifiers, constants, strings, and operators.
2. What are the different types of tokens in C?
- Answer: The main types of tokens in C are:
- Keywords (e.g.,
int
, return
, if
)
- Identifiers (e.g., variable names like
age
, num
)
- Constants (e.g., numeric values like
100
, or character constants like 'a'
)
- Strings (e.g.,
"Hello, World!"
)
- Operators (e.g.,
+
, -
, *
, &&
)
3. What is a keyword in C?
- Answer: A keyword is a reserved word that has a special meaning in C. It cannot be used as an identifier (e.g.,
int
, float
, return
).
4. What are identifiers in C?
- Answer: Identifiers are names given to various program elements like variables, functions, and arrays. They must begin with a letter or underscore and can contain letters, digits, and underscores.
5. What are constants in C?
- Answer: Constants are values that cannot be changed during program execution. They can be:
- Integer constants (e.g.,
5
, -7
)
- Floating-point constants (e.g.,
3.14
, 0.0001
)
- Character constants (e.g.,
'a'
, '7'
)
6. What are string literals in C?
- Answer: A string literal is a sequence of characters enclosed in double quotes (e.g.,
"Hello"
). It is treated as a constant.
7. What are operators in C?
- Answer: Operators are symbols used to perform operations on variables and values. Common categories include:
- Arithmetic operators (
+
, -
, *
, /
, %
)
- Relational operators (
==
, !=
, >
, <
, >=
, <=
)
- Logical operators (
&&
, ||
, !
)
- Bitwise operators (
&
, |
, ^
, <<
, >>
)
8. What is the difference between float
and double
data types in C?
- Answer: Both
float
and double
are used to represent floating-point numbers, but:
float
is 4 bytes and has less precision (6-7 digits).
double
is 8 bytes and offers more precision (15-16 digits).
9. What is the char
data type in C?
- Answer: The
char
data type is used to store single characters. It occupies 1 byte of memory and can store values from -128
to 127
(signed) or 0
to 255
(unsigned).
10. What are the size limits of the int
data type in C?
- Answer: The size of
int
depends on the system architecture:
- Typically 4 bytes on most modern systems.
- Ranges from
-2,147,483,648
to 2,147,483,647
for a signed int
.
11. What is the printf()
function in C?
- Answer: The
printf()
function is used for output in C. It can print text, variables, or formatted values to the console. Example: printf("Value is: %d", value);
12. What is the scanf()
function in C?
- Answer: The
scanf()
function is used for input. It allows the user to enter values, which are then stored in specified variables. Example: scanf("%d", &num);
13. What is the difference between scanf()
and printf()
?
- Answer:
scanf()
is used for reading input from the user, while printf()
is used for displaying output to the user.
14. What are format specifiers in C?
- Answer: Format specifiers are placeholders used in
printf()
and scanf()
to specify the type of data. Example: %d
for integers, %f
for floats, %c
for characters, %s
for strings.
15. What is the difference between =
, ==
, and ===
in C?
- Answer: In C:
=
is the assignment operator used to assign a value.
==
is the equality operator used to compare values.
===
is not a valid operator in C (used in JavaScript).
16. What is the use of the sizeof()
operator in C?
- Answer: The
sizeof()
operator is used to get the size of a data type or variable in bytes. Example: sizeof(int)
returns the size of an integer.
17. What are the different types of constants in C?
- Answer: Constants in C can be:
- Integer constants (e.g.,
5
, -10
)
- Floating-point constants (e.g.,
3.14
, -0.001
)
- Character constants (e.g.,
'a'
, '1'
)
- String constants (e.g.,
"Hello"
)
18. What are the basic rules for defining variables in C?
- Answer: Rules for defining variables in C include:
- Variables must start with a letter or an underscore (
_
).
- Variables can contain letters, digits, and underscores.
- Variables are case-sensitive.
- Variables must not be C keywords.
19. How do you declare a constant in C?
- Answer: Constants are declared using the
const
keyword. Example: const int MAX = 100;
20. What is the difference between int
and long
in C?
- Answer:
int
typically holds 4 bytes, while long
usually holds 8 bytes on 64-bit systems. long
is used for larger integer values.
21. What is type casting in C?
- Answer: Type casting is converting a variable from one data type to another. It can be done explicitly using type casting operators. Example:
(float)5
casts the integer 5
to a float.
22. What are implicit and explicit type conversions?
- Answer: Implicit type conversion is done automatically by the compiler (e.g.,
int
to float
). Explicit type conversion is done manually by the programmer (e.g., (float)num
).
23. What is the use of the return
statement in a function?
- Answer: The
return
statement is used to exit from a function and optionally return a value to the caller.
24. How can you initialize a variable in C?
- Answer: Variables can be initialized during declaration. Example:
int num = 10;
25. What is the purpose of comments in C?
- Answer: Comments are used to make code more readable and provide explanations. They are ignored by the compiler.
26. What is a null
pointer in C?
- Answer: A
null
pointer is a pointer that does not point to any valid memory location. It is initialized with the value NULL
.
27. What is the difference between NULL
and 0
in C?
- Answer:
NULL
is a symbolic constant used to represent a null pointer, while 0
is a numeric value.
28. What is the use of the break
statement in C?
- Answer: The
break
statement is used to exit from a loop or switch
statement prematurely.
29. What is the use of the continue
statement in C?
- Answer: The
continue
statement is used to skip the current iteration of a loop and proceed to the next iteration.
30. What are logical operators in C?
- Answer: Logical operators are used to combine conditional statements:
&&
(logical AND)
||
(logical OR)
!
(logical NOT)