All Exams  >   Class 10  >   C++ Programming for Beginners  >   All Questions

All questions of C++ Tutorial for Class 10 Exam

How many parameters can a resize method take?
  • a)
    1
  • b)
    2
  • c)
    1 or 2
  • d)
    0
Correct answer is option 'C'. Can you explain this answer?

Harshita jain answered
Explanation:

A resize method is a method that is used to change the size of an object or data structure. The number of parameters that a resize method can take depends on the specific implementation and requirements of the method.

Parameters in a method:
Parameters in a method are variables that are used to pass values to the method. They define the data that the method requires to perform its functionality. The number of parameters in a method determines how many values need to be passed to the method when it is called.

Number of parameters in a resize method:
The number of parameters in a resize method can vary depending on the specific implementation and functionality required. However, in general, a resize method may take one or two parameters.

Option 'A': 1 parameter
A resize method may take one parameter if it only needs to specify the new size of the object or data structure. For example, a resize method for an array may take a single parameter representing the new size of the array.

Option 'B': 2 parameters
A resize method may also take two parameters if it needs to specify both the new size and a resize strategy or algorithm. The additional parameter could be used to provide information on how the resizing should be performed. For example, a resize method for an image may take two parameters - the new size of the image and the resizing algorithm to be used.

Option 'C': 1 or 2 parameters
Therefore, the correct answer is option 'C' which states that a resize method can take either 1 or 2 parameters depending on the specific implementation and requirements of the method.

Option 'D': 0 parameters
It is also possible for a resize method to not take any parameters. In this case, the method may use internal data or properties to determine the new size or resizing strategy.

Overall, the number of parameters in a resize method can vary based on the specific implementation and functionality required.

Which operator is having the right to left associativity in the following?
  • a)
    Array subscripting
  • b)
    Function call
  • c)
    Addition and subtraction
  • d)
    Type cast
Correct answer is option 'D'. Can you explain this answer?

Seema Gupta answered
Right to Left Associativity of Operators

In programming languages, associativity determines the order in which operators of the same precedence level are evaluated. There are three types of associativity: left to right, right to left, and non-associative.

Right to left associativity means that the operators are evaluated from right to left. This means that the rightmost operator is evaluated first, followed by the operator to its left, and so on.

Among the given options, only the type cast operator has right to left associativity.

Explanation:
The type cast operator is used to convert one data type to another. It has the highest precedence among the operators. When used, it is written as `(type) expression`. The expression inside the parentheses is converted to the specified type.

Example:
```
int a = 10;
double b = (double) a;
```
In the above example, the expression `a` is cast to a double using the type cast operator `(double)`. The result is stored in the variable `b`. The type cast operator has right to left associativity, so the expression `(double) a` is evaluated first, converting the integer value 10 to a double value 10.0.

Associativity of Other Operators:
- Array subscripting operator: The array subscripting operator `[]` is used to access elements of an array. It has left to right associativity. For example, `array[3]` accesses the element at index 3 of the array.
- Function call operator: The function call operator `()` is used to call a function and pass arguments to it. It has left to right associativity. For example, `function(arg1, arg2)` calls the function `function` with arguments `arg1` and `arg2`.
- Addition and subtraction operators: The addition `+` and subtraction `-` operators have left to right associativity. For example, `a + b - c` is evaluated as `(a + b) - c`.

In conclusion, among the given operators, only the type cast operator has right to left associativity.

Evaluate the following.
(false && true) || false || true
  • a)
    0
  • b)
    1
  • c)
    false
  • d)
    2
Correct answer is option 'B'. Can you explain this answer?

Kiran Reddy answered
The given expression is equivalent to
[( false AND True) OR false OR true] This is OR or three values so if any of them will be true then the whole exp will be true and as we have last value as true so the answer of expression will be TRUE.

The correct statement for a function that takes pointer to a float, a pointer to a pointer to a char and returns a pointer to a pointer to a integer is ____________
  • a)
    int **fun(float**, char**)
  • b)
    int *fun(float*, char*)
  • c)
    int **fun(float*, char**)
  • d)
    int ***fun(*float, **char)
Correct answer is option 'C'. Can you explain this answer?

Answer:

The correct statement for a function that takes a pointer to a float, a pointer to a pointer to a char, and returns a pointer to a pointer to an integer is option 'C': int **fun(float*, char**).

Explanation:

Let's break down the function declaration:

- The function takes a pointer to a float as the first parameter. This is represented by the syntax `float*`, which means a pointer to a float.
- The function also takes a pointer to a pointer to a char as the second parameter. This is represented by the syntax `char**`, which means a pointer to a pointer to a char. The double asterisks indicate that we are dealing with a pointer to a pointer.
- The function returns a pointer to a pointer to an integer. This is represented by the syntax `int**`, which means a pointer to a pointer to an integer.

Summary:

In summary, the correct statement for a function that takes a pointer to a float, a pointer to a pointer to a char, and returns a pointer to a pointer to an integer is `int **fun(float*, char**)`. This declaration specifies the correct types for the function parameters and return value, ensuring that the function can be called correctly and that the correct data types are being used.

What will be the output of the following C++ code?
    #include <stdio.h>
    #include<iostream>
    using namespace std;
    int array1[] = {1200, 200, 2300, 1230, 1543};
    int array2[] = {12, 14, 16, 18, 20};
    int temp, result = 0;
    int main()
    {
        for (temp = 0; temp < 5; temp++) 
        {
            result += array1[temp];
        }
        for (temp = 0; temp < 4; temp++)
        {
            result += array2[temp];
        }
        cout << result;
        return 0;
    }
  • a)
    6553
  • b)
    6533
  • c)
    6522
  • d)
    12200
Correct answer is option 'B'. Can you explain this answer?



Explanation:

Initialization:
- Two arrays are defined, array1 and array2, containing integer values.
- A variable "temp" and "result" are defined and initialized to 0.

Calculating the sum:
- The first loop runs from 0 to 4, adding each element of array1 to the result.
- The second loop runs from 0 to 3, adding each element of array2 to the result.

Final result:
- Adding all the elements of array1 and array2 gives the final result.
- The sum of all elements in array1 is 7213, and the sum of all elements in array2 is 80.
- Therefore, the final result is 7213 + 80 = 7293.

Therefore, the output of the code will be 7293.

Is bool a fundamental data type in C++?
  • a)
    Yes
  • b)
    No, it is a typedef of unsigned char
  • c)
    No, it is an enum of {false, true}
  • d)
    No, it is expanded from macros
Correct answer is option 'A'. Can you explain this answer?

Pragya verma answered
Yes, bool is a fundamental data type in C.

In C programming language, bool is a data type that represents a boolean value, which can be either true or false. It is used to store logical values and is an essential part of programming as it allows us to make decisions based on conditions.

Here are some key points to understand why bool is a fundamental data type in C:

1. Definition:
- The bool data type is defined in the C standard library as a fundamental type.
- It is not a typedef or an enum, but a distinct data type specifically designed to store boolean values.

2. Values:
- A bool variable can hold two possible values: true or false.
- The value "true" represents a logical true or a nonzero value, while "false" represents a logical false or a zero value.

3. Size:
- The size of a bool variable is implementation-defined, but it is typically 1 byte.
- It is important to note that the size of a bool is not necessarily the same as the size of an unsigned char.

4. Operations:
- The bool data type supports logical operations such as logical AND, logical OR, and logical NOT.
- These operations allow us to combine and manipulate boolean values to make decisions and control the flow of a program.

5. Standard Headers:
- The bool data type is defined in the C standard library header file "stdbool.h".
- Including this header allows us to use bool and its associated values in our C programs.

6. Usage:
- bool is commonly used in conditional statements, loops, and functions that return boolean values.
- It provides a concise and readable way of expressing logical conditions in our code.

In conclusion, bool is a fundamental data type in C that allows us to work with boolean values. It is not a typedef of unsigned char or an enum, but a distinct data type designed for storing and manipulating logical values. Knowing how to use bool effectively is essential for writing correct and efficient C programs.

Find the odd one out.
  • a)
    std::vector<int>
  • b)
    std::vector<short>
  • c)
    std::vector<long>
  • d)
    std::vector<bool>
Correct answer is option 'D'. Can you explain this answer?

B) std::list
c) std::queue
d) std::map

Answer: c) std::queue (it is a container adapter, not a container like the others)

What will be the output of the following C++ code?
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
        string str ("Steve jobs");
        cout << str.capacity() << "\n";
        return 0;
    }
  • a)
    9
  • b)
    10
  • c)
    11
  • d)
    Not Fix
Correct answer is option 'D'. Can you explain this answer?

Vaibhav Shah answered
Explanation:

String Capacity:
- The capacity of a string is the amount of memory allocated to a string to store characters.
- It is not necessary that the capacity of a string will be equal to the length of the string.
- The capacity of a string is implementation-dependent and may vary from system to system.

Output of the code:
- In the given code, the string "Steve jobs" is created.
- The capacity() function returns the capacity of the string "str".
- The output of the code will depend on the implementation of the string class in the compiler being used.
- Therefore, it is not possible to predict the exact output without knowledge of the specific compiler being used.

Correct Answer:
- Option 'D' is the correct answer as the output cannot be determined without knowing the specific implementation details of the string class in the compiler.

What is this operator called ?:?
  • a)
    conditional
  • b)
    relational
  • c)
    casting operator
  • d)
    unrelational
Correct answer is option 'A'. Can you explain this answer?

Sushant Kapoor answered
Conditional Operator
The operator ?: is known as the conditional operator in programming. It is a ternary operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is true, followed by a colon (:), and finally an expression to execute if the condition is false.

Usage
The conditional operator is often used as a shorthand for an if-else statement. It allows for concise and readable code when you need to make a decision based on a condition.

Example
Here's an example of how the conditional operator is used in a simple programming statement:
c
int x = 10;
int y = (x > 5) ? 10 : 5; // if x is greater than 5, y will be assigned 10, otherwise 5
In this example, the condition `x > 5` is evaluated. If it is true, the value 10 is assigned to y; otherwise, the value 5 is assigned.

Benefits
- Concise code: The conditional operator helps reduce the amount of code needed compared to using an if-else statement.
- Readability: It can make the code easier to read and understand, especially for simple conditional assignments.

Conclusion
In conclusion, the conditional operator ?: is a useful tool in programming for making decisions based on a condition. It provides a compact and efficient way to handle conditional expressions in a concise and readable manner.

Which one of the following is not a possible state for a pointer.
  • a)
    hold the address of the specific object
  • b)
    point one past the end of an object
  • c)
    zero
  • d)
    point to a type
Correct answer is option 'D'. Can you explain this answer?

Amar Patel answered
Explanation:

A pointer is a variable that holds the memory address of another variable or object. It allows us to indirectly access and manipulate the data stored in that memory address.

Possible States for a Pointer:
a) hold the address of the specific object: A pointer can hold the memory address of a specific object. This allows us to access and modify the data stored in that object through the pointer.

b) point one past the end of an object: Pointers can also point to the memory location just beyond the end of an object. This is useful when working with arrays or when iterating over memory blocks.

c) zero: A pointer can have the value of zero, which is also known as a null pointer. A null pointer does not point to any object or memory location. It is often used to indicate that a pointer is not currently pointing to a valid object.

Not a Possible State for a Pointer:
d) point to a type: Pointers do not point to a type itself. They point to the memory address of an object of that type. A pointer is declared with a specific type so that the compiler knows the size and properties of the object being pointed to.

For example, if we have an integer variable `num`, we can declare a pointer to it as `int* ptr = #`. Here, `ptr` holds the memory address of the `num` variable, allowing us to access and manipulate its value indirectly.

In summary, a pointer can hold the address of a specific object, point one past the end of an object, or be null. However, it does not point to a type itself.

What does the following statement mean?
int (*fp)(char*)
  • a)
    pointer to a pointer
  • b)
    pointer to an array of chars
  • c)
    pointer to function taking a char* argument and returns an int
  • d)
    function taking a char* argument and returning a pointer to int
Correct answer is option 'C'. Can you explain this answer?

Leena menon answered
Explanation:

int (*fp)(char*)
- The statement declares a pointer named fp that points to a function.
- The function takes a char* argument and returns an int.

Breaking down the statement:
- int: Specifies the return type of the function pointed to by fp.
- (*fp): Indicates that fp is a pointer to a function.
- (char*): Represents the argument type that the function takes, in this case, a char*.

Interpreting the statement:
- This statement means that fp is a pointer to a function that takes a char* argument and returns an int.
- It does not represent a pointer to a pointer, an array of chars, or a function returning a pointer to an int.

Conclusion:
- In summary, the statement int (*fp)(char*) declares a pointer fp to a function that takes a char* argument and returns an int.

Which of the following gives the memory address of the first element in array?
  • a)
    array[0];
  • b)
    array[1];
  • c)
    array(2);
  • d)
    array;
Correct answer is option 'D'. Can you explain this answer?

Shaina Menon answered
The correct answer is option 'D' - array.

Explanation:
The memory address of the first element in an array can be obtained by simply using the name of the array without any index. Let's understand this in detail:

- In an array, elements are stored in contiguous memory locations. Each element in the array has a unique index starting from 0.
- The name of an array itself represents the memory address of the first element in the array.
- When we use the name of the array without any index, it refers to the memory address of the first element.

Let's consider an example to understand this better. Suppose we have an array called "numbers" with 5 elements:

int numbers[5] = {10, 20, 30, 40, 50};

- The memory representation of this array would be something like this:

| Address | Value |
|---------|-------|
| 1000 | 10 |
| 1004 | 20 |
| 1008 | 30 |
| 1012 | 40 |
| 1016 | 50 |

- In this example, the memory address of the first element (10) is 1000.

Now, let's go through the options given in the question:

a) array[0]: This refers to the first element in the array, but it returns the value of the element, not the memory address.

b) array[1]: This refers to the second element in the array, not the first element.

c) array(2): This is not a valid syntax in C++. Parentheses are used for function calls, not for accessing array elements.

d) array: This is the correct syntax to obtain the memory address of the first element in the array.

Therefore, the correct answer is option 'D' - array, as it represents the memory address of the first element in the array.

What will happen in the following C++ code snippet?
   int a = 100, b = 200;
   int *p = &a, *q = &b;
   p = q;
  • a)
    b is assigned to a
  • b)
    p now points to b
  • c)
    a is assigned to b
  • d)
    q now points to a
Correct answer is option 'B'. Can you explain this answer?

Khushi joshi answered
There is an error in the code snippet. It is incomplete and does not define what value the pointer "p" should be initialized to. Therefore, it is impossible to determine what will happen with the given information.

Which of the following statement(s) is/are correct?
  • a)
    * operator is used to declare a reference
  • b)
    A reference variable defined to refer a particular variable can refer to any other variable also
  • c)
    References must always be initialized inside classes
  • d)
    A variable can have more than one references
Correct answer is option 'D'. Can you explain this answer?

Vidhi bajaj answered
Explanation:

a) * operator is used to declare a reference:
This statement is incorrect. The * operator is used to declare a pointer, not a reference. References are declared using the & operator. For example:
```
int num = 10;
int &ref = num; // declaring a reference
```

b) A reference variable defined to refer a particular variable can refer to any other variable also:
This statement is correct. Once a reference variable is defined to refer to a particular variable, it can later refer to any other variable of the same type. For example:
```
int num1 = 10;
int num2 = 20;

int &ref = num1; // ref refers to num1
ref = num2; // ref now refers to num2, num1 is unchanged
```
In the above example, initially the reference variable `ref` is referring to `num1`. But later, it is assigned to `num2`, so now `ref` refers to `num2` instead.

c) References must always be initialized inside classes:
This statement is incorrect. References can be declared and initialized inside classes, but they are not limited to being initialized only inside classes. References can be declared and initialized in any scope where variables are allowed. For example:
```
void function() {
int num = 10;
int &ref = num; // reference initialized inside a function
}
```
In the above example, the reference variable `ref` is declared and initialized inside the `function()`.

d) A variable can have more than one references:
This statement is correct. A variable can have multiple references pointing to it. This means that multiple reference variables can be declared and initialized to refer to the same variable. Any changes made through one reference will be reflected in the original variable and can be accessed through other references as well. For example:
```
int num = 10;
int &ref1 = num; // first reference
int &ref2 = num; // second reference

ref1 = 20; // num and ref2 will also be 20
```
In the above example, both `ref1` and `ref2` are references to `num`. So any changes made through `ref1` will also be reflected in `num` and `ref2`.

What will be the output of the following C++ code?
    #include <stdio.h>
    #include<iostream>
    using namespace std;
    int main ()
    {
        int array[] = {0, 2, 4, 6, 7, 5, 3};
        int n, result = 0;
        for (n = 0; n < 8; n++) 
        {
            result += array[n];
        }
        cout << result;
        return 0;
    }
  • a)
    25
  • b)
    26
  • c)
    27
  • d)
    21
Correct answer is option 'C'. Can you explain this answer?

Shambavi rao answered

Explanation:

Initialization:
- An integer array named 'array' is initialized with values {0, 2, 4, 6, 7, 5, 3}.
- An integer variable 'n' and 'result' are initialized to 0.

Loop through array:
- A for loop is used to iterate through the elements of the 'array' from index 0 to 7.
- In each iteration, the value at the current index is added to the 'result' variable.

Calculating the result:
- The values in the 'array' are added together in each iteration of the loop.
- The final value of 'result' after the loop completes is 27.

Output:
- The final value of 'result' is printed using cout.
- Therefore, the output of the code will be 27.

What will happen when the structure is declared?
  • a)
    it will not allocate any memory
  • b)
    it will allocate the memory
  • c)
    it will be declared and initialized
  • d)
    it will be declared
Correct answer is option 'A'. Can you explain this answer?

When a structure is declared in a programming language, it refers to the creation of a new user-defined data type. This data type can contain multiple variables of different types, grouped together under a single name. The declaration of a structure does not allocate any memory for the variables within it.

a) It will not allocate any memory:
When a structure is declared, it only defines the blueprint or template for the data type. It specifies the names and types of the variables that will be contained within the structure. However, memory is not allocated at this point.

Explanation:
- The declaration of a structure simply tells the compiler about the existence of a new data type, allowing it to recognize and understand the structure's variables when they are used in the program.
- The purpose of a structure is to provide a way to organize related data. It allows the programmer to create a custom data type that can hold various variables together, making it easier to manage and manipulate data as a whole.
- The structure declaration defines the structure's name and the variables it contains, along with their types. It serves as a blueprint for creating variables of the structure type in the program.
- Memory is not allocated during the structure declaration because the compiler does not know how many instances of the structure will be created or how much memory will be needed for each instance.
- Memory allocation occurs when variables of the structure type are instantiated or initialized using the structure declaration. The memory is allocated based on the size and types of the variables within the structure. This typically happens when a variable of the structure type is declared and initialized separately in the program.

In summary, the declaration of a structure does not allocate any memory. It simply defines the structure's blueprint or template, allowing the programmer to create variables of the structure type. Memory allocation occurs when the variables of the structure type are instantiated or initialized separately in the program.

What are the references in C++?
  • a)
    An alternative name for already existing variables
  • b)
    A pointer to a variable
  • c)
    A new type of variables
  • d)
    A new type of constant variable
Correct answer is option 'A'. Can you explain this answer?

In C programming, references are not directly supported like in other programming languages such as C++ or C#. However, there are certain concepts that can be considered as references in C. Let's explore these concepts in detail:

Introduction:
C is a low-level programming language that does not have built-in support for references. In C, variables are typically accessed by their memory addresses using pointers. However, there are certain situations where certain concepts can be seen as references.

Alternative name for already existing variables:
In C, you can use pointers to create an alternative name for an already existing variable. This can be seen as a form of reference because the pointer holds the address of the original variable and can be used to indirectly access and modify its value. By dereferencing the pointer, you can manipulate the original variable.

Example:
```c
int main() {
int num = 10;
int* ptr = # // ptr is a pointer to num

*ptr = 20; // modifying the value of num indirectly through ptr

printf("%d", num); // Output: 20

return 0;
}
```
In the above example, `ptr` acts as an alternative name for the variable `num`. By dereferencing `ptr` and assigning a new value to it, we are indirectly modifying the value of `num`.

Conclusion:
While references are not directly supported in C, you can use pointers to achieve a similar effect by creating an alternative name for an already existing variable. By manipulating the value of the pointer, you can indirectly modify the original variable.

What is the correct definition of an array?
  • a)
    An array is a series of elements of the same type in contiguous memory locations
  • b)
    An array is a series of element
  • c)
    An array is a series of elements of the same type placed in non-contiguous memory locations
  • d)
    An array is an element of the different type
Correct answer is option 'A'. Can you explain this answer?

Sanaya mehta answered
Definition of an Array:
An array is a series of elements of the same type in contiguous memory locations. Let's break down this definition to understand it better.

Series of Elements:
- An array is a collection of elements that are stored together in a specific order.

Same Type:
- All elements in an array must be of the same data type, such as integers, characters, or strings.

Contiguous Memory Locations:
- In memory, an array stores its elements in adjacent locations. This means that the elements are stored one after the other without any gaps.

Explanation:
- When you declare an array in a programming language, the elements are allocated memory locations that are next to each other.
- This contiguous storage allows for easy access to elements using their index positions.
- By having elements of the same data type and storing them in contiguous memory locations, arrays provide efficient data storage and retrieval capabilities.
In conclusion, an array is a fundamental data structure that organizes elements of the same type in contiguous memory locations, making it a powerful tool for storing and manipulating data in computer programs.

For what values of the expression is an if-statement block not executed?
  • a)
    0 and all negative values
  • b)
    0 and -1
  • c)
    0
  • d)
    0, all negative values, all positive values except 1
Correct answer is option 'C'. Can you explain this answer?

Sagar Malik answered
Explanation:

The if-statement block is a conditional statement that is executed only when a certain condition is met. In this case, the condition is the value of the expression.

Let's analyze each option to determine which values will cause the if-statement block not to be executed:

Option A: 0 and all negative values
- If the expression evaluates to 0 or any negative value, the if-statement block will not be executed.
- This means that if the value is 0 or less than 0, the code inside the if-statement block will not be executed.

Option B: 0 and -1
- If the expression evaluates to 0 or -1, the if-statement block will not be executed.
- This means that if the value is 0 or -1, the code inside the if-statement block will not be executed.

Option C: 0
- If the expression evaluates to 0, the if-statement block will not be executed.
- This means that if the value is 0, the code inside the if-statement block will not be executed.

Option D: 0, all negative values, all positive values except 1
- If the expression evaluates to 0, any negative value, or any positive value except 1, the if-statement block will not be executed.
- This means that if the value is 0, negative, or any positive value except 1, the code inside the if-statement block will not be executed.

Conclusion:
Based on the given options, only option C states that the if-statement block is not executed for the value 0. Therefore, the correct answer is option C.

What is the value of the bool?
  • a)
    True
  • b)
    False
  • c)
    1
  • d)
    2
Correct answer is option 'B'. Can you explain this answer?

Understanding the Value of Bool
In programming, particularly in languages like Python, Java, and C++, a boolean (often abbreviated as "bool") is a data type that can only hold two possible values: true or false. Let's break down the options provided in the question.
Options Explained
- True: This is a valid boolean value, representing a condition that is correct or affirmative.
- False: This is also a valid boolean value, representing a condition that is incorrect or negative.
- 1: In some programming languages, the integer 1 can be interpreted as true, but it is not a boolean value itself.
- 2: Similarly, the integer 2 does not represent a boolean value and can be considered as false in contexts where only 0 (false) and non-zero (true) integers are evaluated.
Correct Answer Clarification
The correct answer provided is option 'B', which states "False". This might lead to confusion since both "True" and "False" are valid boolean values.
However, if the question is specifically asking for the value of a boolean variable that is initialized to false or the expected output of a function that is designed to return a boolean, then "False" would be the correct answer.
Conclusion
- In summary, a boolean can only hold the values of "True" or "False".
- Understanding these values is crucial for logical operations in programming.
- Always remember the distinction between boolean values and integers, as it affects how conditions are evaluated in code.

Choose the right option.
string* x, y;
  • a)
    x is a pointer to a string, y is a string
  • b)
    y is a pointer to a string, x is a string
  • c)
    both x and y are pointers to string types
  • d)
    y is a pointer to a string
Correct answer is option 'A'. Can you explain this answer?

Mehul Saha answered
Pointer to String

Explanation:

In programming, a string is a sequence of characters. A pointer is a variable that stores the memory address of another variable. So, a pointer to a string is a variable that stores the memory address of the first character in a sequence of characters (string).

In this case, variable x is a pointer to a string. It means that x stores the memory address of the first character of a sequence of characters (string). On the other hand, variable y is simply a string. It means that y is a sequence of characters, and it does not store any memory address.

Option A: x is a pointer to a string, y is a string.
This option is correct because x is a pointer that stores the memory address of the first character of a sequence of characters (string). On the other hand, y is simply a sequence of characters.

Option B: y is a pointer to a string, x is a string.
This option is incorrect because y is not a pointer, it is simply a string. On the other hand, x is a pointer that stores the memory address of the first character of a sequence of characters (string).

Option C: Both x and y are pointers to string types.
This option is incorrect because y is not a pointer, it is simply a string. Only x is a pointer that stores the memory address of the first character of a sequence of characters (string).

Option D: y is a pointer to a string.
This option is incorrect because y is not a pointer, it is simply a string.

Which of the following accesses a variable in structure *b?
  • a)
    b->var;
  • b)
    b.var;
  • c)
    b-var;
  • d)
    b>var;
Correct answer is option 'A'. Can you explain this answer?

Mythili jain answered
None of the options provided access a variable in structure *b.

To access a variable in structure *b, we would need to dereference the pointer using the asterisk (*) operator. Additionally, we can use the dot (.) operator to access the variable within the structure.

The correct syntax would be:
(*b).variable

This can also be written using the arrow (->) operator as a shorthand:
b->variable

What will be the output of the following C++ code?
#include<iostream>
using namespace std;
 
int &fun()
{
    static int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}
  • a)
    30
  • b)
    10
  • c)
    Error
  • d)
    Segmentation fault
Correct answer is option 'A'. Can you explain this answer?

Kiran Reddy answered
A function returning value by reference can be used as lvalue i.e. it can be used on the left side of an expression. Here when we are doing fun() = 30 then we are changing the value of x(i.e. value returning) to and as x is static therefore it will not be initialized again so the value of x becomes 30 hence the output is 30.

What will be the output of the following C++ code?
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    int main()
    {
        char str[5] = "ABC";
        cout << str[3];
        cout << str;
        return 0;
    }
  • a)
    ABC
  • b)
    ABCD
  • c)
    AB
  • d)
    AC
Correct answer is option 'A'. Can you explain this answer?

Snigdha Shah answered
Explanation:
- Initialization:
- The given C++ code initializes a character array `str` with size 5 and assigns the values "ABC" to it.
- Output:
- `str[3]` refers to the 4th element of the array, which is beyond the bounds of the array.
- When we try to output `str[3]`, it will result in undefined behavior as we are accessing memory outside the bounds of the array.
- The output of the code will still be "ABC" because `cout < str;`="" will="" print="" the="" entire="" string="" until="" a="" null="" terminator="" is="" />
Therefore, the correct output will be ABC.

What will be the output of the following C++ code?
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    int main()
    {
        int array[] = {10, 20, 30};
        cout << -2[array];
        return 0;
    }
  • a)
    -15
  • b)
    -30
  • c)
    compile time error
  • d)
    garbage value
Correct answer is option 'B'. Can you explain this answer?


Explanation:

- The expression `-2[array]` is equivalent to `-(2[array])`.
- In C++, array indexing can be done using either `array[index]` or `index[array]`, as they are equivalent due to the commutative property of addition.
- So, `2[array]` is equivalent to `array[2]`, which accesses the element at index 2 in the array.
- In the given array `{10, 20, 30}`, the element at index 2 is `30`.
- Therefore, `-2[array]` is equivalent to `-array[2]`, which is `-30`.
- Hence, the output of the code will be `-30`.

Therefore, the correct answer is option (b) -30.

What will be the output of the following C++ code?
    #include <iostream>
    using namespace std;
    main()
    {
        double a = 21.09399;
        float b = 10.20;
        int c ,d;
        c = (int) a;
        d = (int) b;
        cout << c <<' '<< d;
        return 0;
    }
  • a)
    20 10
  • b)
    10 21
  • c)
    21 10
  • d)
    10 20
Correct answer is option 'C'. Can you explain this answer?

Akshara bajaj answered

Explanation:

Conversion of double to int:
- When a double value is converted to an int, the decimal portion is truncated.
- In this code, the value of 'a' is 21.09399 which is converted to an integer 'c'. So, 'c' will be 21.

Conversion of float to int:
- Similarly, when a float value is converted to an int, the decimal portion is truncated.
- In this code, the value of 'b' is 10.20 which is converted to an integer 'd'. So, 'd' will be 10.

Output:
- As per the above conversions, the output of the code will be:
21 10

Therefore, the correct answer is option C - 21 10.

What will be the output of the following C++ code?
    #include <iostream>
    using namespace std;
    int main()
    {
        int a = 5, b = 6, c, d;
        c = a, b;
        d = (a, b);
        cout << c << ' ' << d;
        return 0;
    }
  • a)
    5  6
  • b)
    6  5
  • c)
    6  7
  • d)
    6  8
Correct answer is option 'A'. Can you explain this answer?

Sushant Kapoor answered
Explanation:

Initialization:
- Two integer variables 'a' and 'b' are initialized with values 5 and 6 respectively.
- Two integer variables 'c' and 'd' are declared.

Assignment:
- In the statement 'c = a, b;' the value of 'a' is assigned to 'c' and then 'b' is discarded.
- In the statement 'd = (a, b);' the comma operator evaluates both 'a' and 'b', but only the value of 'b' is assigned to 'd'.

Output:
- The value of 'c' will be 5 (a value of 'a').
- The value of 'd' will be 6 (a value of 'b').
Therefore, the output of the code will be:
5 6

What will be the output of the following C++ code?
    #include <iostream>
    using namespace std;
    int main()
    {
        int a;
        a = 5 + 3 * 5;
        cout << a;
        return 0;
    }
  • a)
    35
  • b)
    20
  • c)
    25
  • d)
    30
Correct answer is option 'B'. Can you explain this answer?

Sushant Kapoor answered
Explanation:

Order of Operations:
- According to the order of operations in mathematics (PEMDAS: Parentheses, Exponents, Multiplication and Division, Addition and Subtraction), multiplication is performed before addition.
- In this code, the expression "5 + 3 * 5" is evaluated.

Calculation:
- First, 3 * 5 is calculated which equals 15.
- Then, 5 + 15 is calculated which equals 20.
- Therefore, the value of 'a' is 20.
Therefore, the output of the given C++ code will be 20.

Which of the two operators ++ and — work for the bool data type in C++?
  • a)
    None
  • b)
    ++
  • c)
  • d)
    ++ & —
Correct answer is option 'B'. Can you explain this answer?

Pragya jain answered
Both operators are the same. The ">" operator is used to compare if one value is greater than another value, while the ">" operator is used to compare if one value is greater than or equal to another value.

What will be the output of the following C++ code?
    #include <iostream>
    using namespace std;
    struct Time 
    {
        int hours;
        int minutes;
        int seconds;
    };
    int toSeconds(Time now);
    int main()
    {
        Time t;
        t.hours = 5;
        t.minutes = 30;
        t.seconds = 45;
        cout << "Total seconds: " << toSeconds(t) << endl;
        return 0;
    }
    int toSeconds(Time now)
    {
        return 3600 * now.hours + 60 * now.minutes + now.seconds;
    }
  • a)
    19845
  • b)
    20000
  • c)
    15000
  • d)
    19844
Correct answer is option 'A'. Can you explain this answer?

Vani Gupta answered
Explanation:

Main Function:
- In the main function, a variable of type Time named 't' is created and initialized with hours = 5, minutes = 30, and seconds = 45.
- The function toSeconds(t) is called and its return value is printed, which calculates the total seconds from the Time structure.

toSeconds Function:
- The toSeconds function takes a Time structure as a parameter and calculates the total number of seconds.
- It multiplies the hours by 3600 (number of seconds in an hour), the minutes by 60 (number of seconds in a minute), and adds the seconds to get the total seconds.

Calculation:
- Total seconds = 3600 * 5 + 60 * 30 + 45
- Total seconds = 18000 + 1800 + 45
- Total seconds = 19845
Therefore, the output of the code will be:
Total seconds: 19845

Pick the correct statement about references in C++.
  • a)
    References stores the address of variables
  • b)
    References and variables both have the same address
  • c)
    References use dereferencing operator(*) to access the value of variable its referencing
  • d)
    References were also available in C
Correct answer is option 'B'. Can you explain this answer?

Vidhi bajaj answered
Explanation:

In C programming language, references are not directly available like in C++. However, there are ways to achieve similar functionality using pointers. Let's discuss each option and determine the correct statement.

a) References store the address of variables:
This statement is incorrect. In C, references are not a built-in feature, and therefore they do not exist. Instead, pointers are used to store the address of variables.

b) References and variables both have the same address:
This statement is correct. In C, when a pointer is assigned the address of a variable, the pointer and the variable will have the same address. This allows the pointer to indirectly access and modify the value of the variable.

c) References use dereferencing operator(*) to access the value of the variable it's referencing:
This statement is incorrect. In C, to access the value of a variable through a pointer, the dereferencing operator (*) is used. However, since references are not available in C, this statement does not apply.

d) References were also available in C:
This statement is incorrect. References were introduced in C++ and are not available in the C programming language.

Therefore, the correct statement is option b) References and variables both have the same address. This statement reflects the behavior of pointers in C, where a pointer stores the address of a variable, and both the pointer and the variable have the same address.

What will be the output of the following C++ code?
    #include <iostream>
    using namespace std;
    int main()
    {
        int a = 5, b = 10, c = 15;
        int *arr[ ] = {&a, &b, &c};
        cout << arr[1];
        return 0;
    }
  • a)
    5
  • b)
    10
  • c)
    15
  • d)
    it will return some random number
Correct answer is option 'D'. Can you explain this answer?

Pooja nair answered


Explanation:

There are a few key points to understand about the given code:

- The code declares three integer variables a, b, and c with values 5, 10, and 15 respectively.
- An array of integer pointers arr is declared and initialized with the addresses of variables a, b, and c.
- When the statement `cout < arr[1];`="" is="" executed,="" it="" will="" output="" the="" address="" of="" the="" second="" element="" in="" the="" arr="" array,="" which="" is="" the="" address="" of="" variable="" />
- However, when you try to output the address using `cout`, it will not display the actual address value but will instead display some random number. This is because the address is being treated as an integer value and not as a memory address.

Therefore, the output of the code will be some random number and not the value of variable b.

Correct answer: d) it will return some random number

Chapter doubts & questions for C++ Tutorial - C++ Programming for Beginners 2025 is part of Class 10 exam preparation. The chapters have been prepared according to the Class 10 exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Class 10 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of C++ Tutorial - C++ Programming for Beginners in English & Hindi are available as part of Class 10 exam. Download more important topics, notes, lectures and mock test series for Class 10 Exam by signing up for free.

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev