C and C++ Coding 1 — Questions and Answers
Question 1: Cout is an object.
- True (Correct answer)
- False
Correct answer: True
In C++, `cout` is indeed an object, specifically an object of the `ostream` class. It is part of the `iostream` library and is used for sending data to the standard output device, typically the console. As an object, `cout` has various member functions and properties that facilitate formatted output.
Question 2: If an is an integer variable, a=7/3; will yield a result of
- 2 (Correct answer)
- 0
- 2.53
- 3
Correct answer: 2
When performing integer division in many programming languages (like C, C++, or Java), if both operands are integers, the result will also be an integer, and any fractional part is truncated. For `a = 7 / 3;`, the mathematical result is approximately 2.33. Since `a` is an integer variable, the `.33` is discarded, yielding `2`.
Question 3: Which of the statements below is true?
- Strcmp(s1, s2) returns 1 if s1==s2
- Strcmp(s1, s2) returns 0 if s1==s2 (Correct answer)
- strcmp(s1, s2) returns a number greater than 0 if s1
- Strcmp(s1, s2) returns a number less than 0 if s1>s2
Correct answer: Strcmp(s1, s2) returns 0 if s1==s2
The `strcmp` function in C/C++ is used to compare two strings lexicographically. It returns `0` if the two strings, `s1` and `s2`, are identical. A negative value is returned if `s1` is lexicographically less than `s2`, and a positive value if `s1` is greater than `s2`.
Question 4: What happens when we use a class with parameterized constructors but no default constructor in a program to create an object that requires a zero-argument constructor?
- Compile-time error. (Correct answer)
- Preprocessing error
- Runtime error.
- Runtime exception
Correct answer: Compile-time error.
If a class explicitly defines one or more parameterized constructors but no default (zero-argument) constructor, the compiler will not automatically generate a default constructor. Consequently, attempting to create an object of that class without providing any arguments (e.g., `MyClass obj;`) will result in a compile-time error. The compiler cannot find a matching constructor to instantiate the object.
Question 5: The allowable range for integer constants in a 16-bit compiler is __________.
- -32767 to 32768
- -32668 to 32667
- -32768 to 32767 (Correct answer)
- -3.4e38 to 3.4e38
Correct answer: -32768 to 32767
A 16-bit signed integer uses one bit for the sign and 15 bits for the magnitude of the number. This allows it to represent 2^15 positive values (0 to 32767) and 2^15 negative values (-1 to -32768). Therefore, the standard range for a 16-bit signed integer is from -32768 to 32767.
Question 6: Which of the following does NOT qualify as a logical or relational operator?
- != (Correct answer)
- =
- ||
- ==
Correct answer: !=
The `!=` operator (read as 'not equal to') is a relational operator used to compare two values. It evaluates to `true` if the operands are different, and `false` if they are the same. This comparison determines a relationship between the values, making it a key component in conditional statements for checking inequality.
Question 7: Which of the following concepts refers to determining which method to invoke at runtime?
- Dynamic loading
- Data hiding
- Dynamic binding (Correct answer)
- Dynamic Typing
Correct answer: Dynamic binding
Dynamic binding, also known as late binding, is an object-oriented programming concept where the method to be invoked is determined at runtime, not during compilation. This mechanism is crucial for achieving polymorphism, allowing the correct method implementation to be called based on the actual type of the object, rather than its declared type.
Cout is an object.