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

All questions of C++ Tutorial for Class 10 Exam

Which header file is required for manipulation of math functions in c++?
  • a)
    cmath
  • b)
    maths
  • c)
    math
  • d)
    dmath
Correct answer is option 'A'. Can you explain this answer?

Explanation:

Header File for Math Functions:
- The required header file for manipulation of math functions in C++ is cmath.

Usage of cmath:
- The cmath header file in C++ provides a set of functions to perform mathematical operations like square roots, trigonometric functions, logarithms, etc.

Include Directive:
- To use the functions provided by cmath, you need to include the header file at the beginning of your C++ program using the #include directive.

Example:
- Here is an example of using the sqrt() function from cmath to calculate the square root of a number:
cpp
#include
#include
int main() {
double num = 16.0;
double squareRoot = std::sqrt(num);
std::cout < "square="" root="" of="" "="" />< num="" />< "="" is:="" "="" />< squareroot="" />< />
return 0;
}

Conclusion:
- In conclusion, the cmath header file is essential for utilizing math functions in C++ programming. Make sure to include it in your program to access the mathematical functionalities provided by C++.

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 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.

Identify the incorrect statement.
  • a)
    Reference is the alternate name of the object
  • b)
    A reference value once defined can be reassigned
  • c)
    A reference value once defined cannot be reassigned
  • d)
    Reference is the alternate name of the variable
Correct answer is option 'B'. Can you explain this answer?

Akash Kumar answered
Incorrect Statement: A reference value once defined can be reassigned

Explanation:


Definition of a Reference:
A reference is an alternate name given to an object or variable. It allows us to access the object or variable indirectly using the reference name.

Reassignment of a Reference:
A reference value once defined cannot be reassigned. Once a reference is assigned to a specific object or variable, it remains associated with that object or variable throughout its lifetime.

Understanding Reference:
To understand this better, let's consider an example. Suppose we have a variable called "num" which stores the value 5. We can create a reference "ref" and assign it to the variable "num". Now, "ref" becomes an alternate name for "num", and we can access the value of "num" using the reference "ref". If we try to reassign the reference "ref" to another variable or object, it will result in an error.

Example:
```
int num = 5; // variable num with value 5
int &ref = num; // reference ref assigned to num
ref = 10; // value of num changed to 10
```

In the above example, the reference "ref" is assigned to the variable "num". When we change the value of "ref" to 10, it actually modifies the value of "num" as well. This is because "ref" is just an alternate name for "num", not a separate variable.

Conclusion:
Therefore, the correct statement is option 'C' - A reference value once defined cannot be reassigned. Once a reference is assigned to an object or variable, it always refers to that object or variable and cannot be assigned to another one.

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.

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)

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.

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 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 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;
    void swap(int &a, int &b);
    int main()
    {
        int a = 5, b = 10;
        swap(a, b);
        cout << "In main " << a << b;
        return 0;
    }
    void swap(int &a, int &b)
    {
        int temp;
        temp = a;
        a = b;
        b = temp;
        cout << "In swap " << a << b;
    }
  • a)
    In swap 105 In main 105
  • b)
    In swap 105 In main 510
  • c)
    In swap 510 In main 105
  • d)
    In swap 510 In main 510
Correct answer is option 'A'. Can you explain this answer?

Parth Nambiar answered
Explanation:

Main Function:
- Initialize two integers, a=5 and b=10.
- Call the swap function passing the values of a and b.
- Print "In main" followed by the values of a and b which were swapped in the swap function.

Swap Function:
- Declare a temporary variable temp.
- Assign the value of a to temp.
- Assign the value of b to a.
- Assign the value of temp to b.
- Print "In swap" followed by the values of a and b after swapping.
Therefore, the output will be:
In swap 10 5
In main 10 5

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?

Kiran Reddy answered
str.capacity() returns the size of the storage space currently allocated for the string, expressed in terms of bytes and capacity of the string may be equal or greater. So the answer is not fix, depends on the compiler.
Output:
$ g++ basicst1.cpp
$ a.out
10

What will be the output of the following C++ code?
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
        string name ("Jobs");
        string family ("Steve");
        name += " Apple ";
        name += family;
        name += '\n';
        cout << name;
        return 0;
    }
  • a)
    Steve Jobs
  • b)
    Apple
  • c)
    Jobs Apple Steve
  • d)
    Apple Steve
Correct answer is option 'C'. Can you explain this answer?

Parth Menon answered

Explanation:

- The code defines two string variables, `name` and `family`, with the values "Jobs" and "Steve" respectively.
- The code then appends the string " Apple " to the `name` variable using the `+=` operator.
- Next, it appends the `family` variable to the `name` variable.
- The code then tries to append a newline character `\n` to the `name` variable, but it is missing the double quotes, which will result in a compilation error.
- Finally, the content of the `name` variable is printed using `cout`.

Output:
The output of the code will be "Jobs Apple Steve" because the newline character `\n` is not correctly appended to the string.

What will be the output of the following C++ code?
    #include <iostream>
    #include <string.h>
    using namespace std;
    int main()
    {
        struct student 
        {
            int num;
            char name[25];
        };
        student stu;
        stu.num = 123;
        strcpy(stu.name, "John");
        cout << stu.num << endl;
        cout << stu.name << endl;
        return 0;
    }
  • a)
    123
    john
  • b)
    john
    john
  • c)
    compile time error
  • d)
    runtime error
Correct answer is option 'A'. Can you explain this answer?

Explanation:

Struct Definition:
- The given C++ code defines a structure named 'student' with two members: an integer 'num' and a character array 'name' of size 25.

Main Function:
- Inside the 'main' function, an object 'stu' of type 'student' is created.
- The 'num' member of 'stu' is assigned a value of 123.
- The 'name' member of 'stu' is assigned the string "John" using the 'strcpy' function.

Output:
- The code then prints the value of 'stu.num' which is 123, followed by the value of 'stu.name' which is "John".
- Therefore, the output of the code will be:
123
John

Conclusion:
- The code successfully assigns values to the members of the 'student' structure object 'stu' and prints them to the console.

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 will be the output of the following C++ code?
    #include <iostream>
    using namespace std;
    int main()
    {
        struct ShoeType 
        {
           string style;
           double price;
        };
         ShoeType shoe1, shoe2;
         shoe1.style = "Adidas";
         shoe1.price = 9.99;
         cout << shoe1.style << " $ "<< shoe1.price;
         shoe2 = shoe1;
         shoe2.price = shoe2.price / 9;
         cout << shoe2.style << " $ "<< shoe2.price;
         return 0;
    }
  • a)
    Adidas $ 9.99Adidas $ 1.11
  • b)
    Adidas $ 9.99Adidas $ 9.11
  • c)
    Adidas $ 9.99Adidas $ 11.11
  • d)
    Adidas $ 11.11Adidas $ 11.11
Correct answer is option 'A'. Can you explain this answer?

Arpita reddy answered
Explanation:
ShoeType Structure:
- The code defines a structure named ShoeType with two members: style (string) and price (double).
Initialization:
- Two ShoeType variables, shoe1 and shoe2, are declared.
- shoe1 is initialized with style "Adidas" and price 9.99.
Output for shoe1:
- The first cout statement prints "Adidas $ 9.99" as the output.
Copying shoe1 to shoe2:
- shoe2 is assigned the values of shoe1 using the assignment operator.
- The price of shoe2 is updated by dividing it by 9 (9.99 / 9 = 1.11).
Output for shoe2:
- The second cout statement prints "Adidas $ 1.11" as the output.
Final Output:
- Therefore, the final output of the code will be "Adidas $ 9.99" followed by "Adidas $ 1.11", which corresponds to option 'A'.

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.

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 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.

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.

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.

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.

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.

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.

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.

Which of the following is a properly defined structure?
  • a)
    struct {int a;}
  • b)
    struct a_struct {int a;}
  • c)
    struct a_struct int a;
  • d)
    struct a_struct {int a;};
Correct answer is option 'D'. Can you explain this answer?

Kunal Khanna answered
Properly Defined Structure:
A properly defined structure in C programming is a user-defined data type that allows you to combine different data types under a single name. It is defined using the "struct" keyword followed by a struct tag, which is an optional identifier for the structure, and a list of member variables enclosed in braces.

The correct option among the given choices is option D, which is:
struct a_struct {int a;};

Explanation:
Let's break down the options and understand why option D is the correct answer.

a) struct {int a;};
This option defines an unnamed structure with a single member variable "a" of type int. While this is a valid structure declaration, it is not a properly defined structure because it lacks a struct tag. A struct tag is essential for future references to the structure.

b) struct a_struct {int a;};
This option defines a structure named "a_struct" with a single member variable "a" of type int. This is a properly defined structure as it includes a struct tag and specifies the member variable.

c) struct a_struct int a;
This option is not a valid structure definition. It seems to be a combination of a structure definition and variable declaration. The correct syntax for declaring a variable of a structure type would be:
struct a_struct variable_name;

d) struct a_struct {int a;};
This option defines a structure named "a_struct" with a single member variable "a" of type int. This is the correct and properly defined structure as it includes a struct tag and specifies the member variable.

In summary, option D is the correct answer because it defines a structure named "a_struct" with a member variable "a" of type int, adhering to the proper syntax of a structure definition.

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

Pooja nair answered

Explanation:

- The code provided is trying to create an integer array `arr` with three elements, where each element points to the variables `a`, `b`, and `c`.
- In C++, when we declare an array of integers, we should only use integer values within the curly braces. However, in this code, the elements of the array are pointers to integers.
- This will result in a compile-time error because the compiler expects integer values, not pointers.
- Therefore, the code will not compile, and the output will be a compile-time error.

Therefore, the correct answer is compile time error.

What will be the output of the following C++ code?
    #include <iostream>
    using namespace std;
    int main()
    {
        int a = 9;
        int & aref = a;
        a++;
        cout << "The value of a is " << aref;
        return 0;
    }
  • a)
    9
  • b)
    10
  • c)
    error
  • d)
    11
Correct answer is option 'B'. Can you explain this answer?

Sonia rao answered
Explanation:

Reference Variable:
- In the given code, a reference variable `aref` is created which is referring to the variable `a`.
- The reference variable `aref` is essentially an alias for the variable `a`.

Incrementing the Value:
- The value of the variable `a` is incremented by 1 using the `a++` statement.
- After incrementing, the value of `a` becomes 10.

Output:
- When we print the value of `aref`, it will display the updated value of `a`, which is 10.
- Therefore, the output of the code will be "The value of a is 10".
Therefore, the correct answer is option 'B'.

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

Kiran Reddy answered
In this operator, if the condition is true means, it will return the first operator, otherwise second operator.

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 is the use of dynamic_cast operator?
  • a)
    it converts virtual base class to derived class
  • b)
    it converts the virtual base object to derived objects
  • c)
    it will convert the operator based on precedence
  • d)
    it converts the virtual base object to derived class
Correct answer is option 'A'. Can you explain this answer?

Riddhi singh answered




Introduction:
Dynamic_cast operator is a type of casting operator in C++ that is used for converting a pointer or reference from a virtual base class to a derived class. It is primarily used for downcasting, i.e., converting a base class pointer or reference to a derived class pointer or reference.

Usage:

  • Converts virtual base class to derived class: The main use of dynamic_cast operator is to safely convert a pointer or reference of a virtual base class to a pointer or reference of a derived class. This is particularly useful when dealing with polymorphic objects and you need to access specific members or functions of the derived class.



Example:
Consider a scenario where you have a base class Animal and two derived classes Dog and Cat. If you have a pointer of type Animal that actually points to an object of type Dog, you can use dynamic_cast to safely convert this pointer to a Dog pointer.

Syntax:
```cpp
Dog* dogPtr = dynamic_cast(animalPtr);
if(dogPtr) {
// safe to use dogPtr
} else {
// conversion failed
}
```

Benefits:

  • Ensures type safety during runtime conversions

  • Helps in avoiding undefined behavior when casting pointers or references

  • Provides a way to check if the conversion was successful



In conclusion, the dynamic_cast operator in C++ is a powerful tool for safely converting pointers or references from a base class to a derived class, helping in maintaining type safety and avoiding runtime errors. It is a valuable feature for working with polymorphic objects and class hierarchies in C++ programming.


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.

Which of the following is illegal?
  • a)
    int *ip;
  • b)
    string s, *sp = 0;
  • c)
    int i; double* dp = &i;
  • d)
    int *pi = 0;
Correct answer is option 'C'. Can you explain this answer?

Nisha dubey answered
Understanding the Options
When analyzing the legality of the given code snippets, it's essential to understand the data types and how pointers work in C/C++.
Option A: int *ip;
- This declares a pointer to an integer, which is perfectly legal.
Option B: string s, *sp = 0;
- This declares a string and a pointer to a string initialized to null. This is also legal.
Option C: int i; double* dp = &i;
- This option is illegal. Here’s why:
- `int i;` declares an integer variable.
- `double* dp = &i;` attempts to assign the address of an integer variable to a pointer meant for doubles. This is a type mismatch since an integer cannot be treated as a double.
Option D: int *pi = 0;
- This declares a pointer to an integer initialized to null. This is legal and commonly done to indicate that the pointer is not currently pointing to any valid memory.
Conclusion
In summary, option C is illegal due to the type mismatch between the integer variable and the double pointer. Pointers must match the type of the data they point to, making this assignment invalid. Understanding these fundamentals is crucial for working with pointers in C/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.

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.

Top Courses Class 10

Related Class 10 Content