Free Bachelor of Computer Engineering C Language Questions and Answers — Questions and Answers
Question 1: Who is the C language's father?
- Dennis Ritchie (Correct answer)
- Rasmus Lerdorf
- Steve Jobs
- James Gosling
Correct answer: Dennis Ritchie
Dennis Ritchie is widely recognized as the creator of the C programming language. He developed C at Bell Labs between 1969 and 1973, primarily for use with the Unix operating system. His work profoundly influenced the development of subsequent programming languages and operating systems.
Question 2: Which of the following is a name for a C variable that is invalid?
- float rate;
- int variable_count;
- int number;
- int $main; (Correct answer)
Correct answer: int $main;
In C, variable names must follow specific rules. They can contain letters, digits, and underscores, but they cannot start with a digit and cannot contain special characters like `$`. The `$` symbol is not permitted in standard C variable identifiers, making `int $main;` an invalid declaration.
Question 3: Which statement regarding variable names in C is accurate?
- It is not an error to declare a variable to be one of the keywords(like goto, static)
- They can contain alphanumeric characters as well as special characters
- Variable can be of any length
- Variable names cannot start with a digit (Correct answer)
Correct answer: Variable names cannot start with a digit
In C programming, a fundamental rule for variable naming is that an identifier cannot begin with a digit. Variable names must start with either an alphabet (uppercase or lowercase) or an underscore (`_`). While they can contain digits later in the name, starting with one is strictly prohibited, ensuring clear distinction from numeric literals.
Question 4: Which C expression is legitimate?
- int my_num = 100000; (Correct answer)
- int my num = 1000;
- int my_num = 100,000;
- int $my_num = 10000;
Correct answer: int my_num = 100000;
In C, variable names can contain underscores and alphanumeric characters, and `my_num` is a valid identifier. Integer literals like `100000` are also valid. Options B and D contain invalid characters (` ` and `$`) in the variable name, while option C uses a comma within the numeric literal, which is not allowed in C for integer values.
Question 5: Which of the following declarations does the C language not support?
- char *str;
- String str; (Correct answer)
- Both “String str;” and “float str = 3e2;”
- float str = 3e2;
Correct answer: String str;
The C language does not have a built-in `String` data type with a capital 'S', unlike some other programming languages like Java or C++. In C, strings are typically handled as arrays of characters (`char[]`) or pointers to characters (`char*`). Therefore, `String str;` is not a valid declaration in standard C.
Question 6: Which keyword is used in a C program to stop a variable from changing at all?
- mutable
- volatile
- immutable
- const (Correct answer)
Correct answer: const
The `const` keyword in C is used to declare a variable as constant, meaning its value cannot be modified after initialization. This ensures that the variable's value remains fixed throughout its scope, preventing accidental changes by the program. It is the primary mechanism for creating read-only variables.
Question 7: What is the name of the C property that enables the creation of several executables for various platforms?
- Selective inclusion
- Recursive macros
- File inclusion
- Conditional compilation (Correct answer)
Correct answer: Conditional compilation
Conditional compilation is a C preprocessor feature that allows different blocks of code to be compiled based on specific conditions, such as the target platform or defined macros. Using directives like `#ifdef`, `#ifndef`, `#if`, `#else`, and `#endif`, developers can include or exclude code sections, enabling the creation of multiple executables tailored for various platforms from a single source file.
Question 8: The ________ symbol is used to specify the C-preprocessors.
- $
- ” ”
- &
- # (Correct answer)
Correct answer: #
The hash symbol (`#`) is used to specify C preprocessor directives. These directives, such as `#include`, `#define`, `#ifdef`, and `#pragma`, are processed by the preprocessor before the main compilation phase. They modify the source code by including files, defining macros, or conditionally compiling code sections.
Question 9: In the header file, there is a predefined function called scanf().
- ctype. h
- stdarg. h
- astdlib. h
- stdio. h (Correct answer)
Correct answer: stdio. h
The `scanf()` function is a standard input function in C, used for reading formatted input from the console. Its declaration, along with other essential input/output functions like `printf()`, is found in the `stdio.h` header file. `stdio.h` stands for 'standard input/output' and is crucial for performing I/O operations in C programs.
Question 10: What will the following C code produce as its output?
- Hello World! 34
- Hello World! followed by a junk value
- Hello World! 1000
- Compile time error (Correct answer)
Correct answer: Compile time error
Without the actual C code, it's impossible to pinpoint the exact reason for a compile-time error. However, common causes for compile-time errors in C include syntax mistakes (e.g., missing semicolons, mismatched parentheses), undeclared variables or functions, type mismatches in assignments or function calls, or incorrect use of operators. The compiler detects these issues before the program can even run.
Question 11: What will the following C code produce as its output?
- 10 20
- 10
- Compile time error (Correct answer)
- Undefined value
Correct answer: Compile time error
Without the actual C code, it's impossible to pinpoint the exact reason for a compile-time error. However, common causes for compile-time errors in C include syntax mistakes (e.g., missing semicolons, mismatched parentheses), undeclared variables or functions, type mismatches in assignments or function calls, or incorrect use of operators. The compiler detects these issues before the program can even run.
Question 12: Which keyword is used in a C program to stop a variable from changing at all?
- mutable
- volatile (Correct answer)
- immutable
- const
Correct answer: volatile
The `volatile` keyword in C tells the compiler that a variable's value can be changed by external factors, such as hardware or another thread, outside the program's direct control. This prevents the compiler from performing optimizations that might assume the variable's value is stable, ensuring that every access to the variable reads its current value from memory. While `const` prevents programmatic changes, `volatile` ensures the compiler doesn't make assumptions about its stability against external changes.
Question 13: What one of the following is untrue?
- A single variable cannot be defined with two different types in the same scope
- A variable refers to a location in memory
- A variable defined once can be defined again with different scope
- A variable must be declared and defined at the same time (Correct answer)
Correct answer: A variable must be declared and defined at the same time
It is untrue that a variable must be declared and defined at the same time in C. A variable can be declared (e.g., `extern int x;`) to inform the compiler about its type and name without allocating memory. The actual definition, which allocates memory, can occur separately in another part of the program or a different file.
Question 14: Which conversion type is not permitted?
- From float to char pointer (Correct answer)
- From negative int to char
- From char to int
- From double to char
Correct answer: From float to char pointer
Directly converting a `float` to a `char*` (character pointer) is generally not permitted in C because they represent fundamentally different types of data and memory interpretations. A `float` stores a floating-point number, while a `char*` is expected to point to a sequence of characters (a string). Such a conversion would be a type mismatch and typically result in a compile-time error or undefined behavior if forced via casting.
Question 15: What will the following C code produce as its output?
- Its not zero (Correct answer)
- Run time error
- Its zero
- None
Correct answer: Its not zero
Without the specific C code, it's difficult to provide a precise explanation. However, if the output is 'Its not zero', it implies that a conditional statement (like an `if` statement) evaluated a non-zero value as true. In C, any non-zero value is considered true in a boolean context, leading to the execution of the 'true' branch of the condition.
Question 16: What will the following C expression's value be? (x = foo()) ! = 1 assuming that foo() yields 2
- 0
- 1 (Correct answer)
- 2
- TRUE
Correct answer: 1
The expression `(x = foo()) != 1` first evaluates `foo()`, which yields 2, and assigns this value to `x`. Then, the comparison `2 != 1` is performed. Since 2 is indeed not equal to 1, the comparison evaluates to true, and in C, a true boolean result is represented by the integer value 1.
Who is the C language's father?