C++ Programming Short & Very Short Questions & Answers Exam Oriented Notes


C++

Very short answer


Chapter 2

 

1.  What is an operator in C++?

An operator in C++ is a symbol that performs a specific operation on one or more operands.

Examples include +, -, *, /, etc., used in arithmetic and logic. (30)

 

2.  What is operator overloading in C++?

Operator overloading in C++ allows you to redefine the way operators work for user-defined types like classes. It helps in performing operations like + on objects. (30)

 

3.  What is the precedence of operators in C++?

Operator precedence in C++ determines the order in which operators are evaluated in an expression. Higher precedence operators are evaluated before lower precedence ones automatically. (30)

 

4.  What is a data type in C++?

A data type in C++ specifies the type of data a variable can hold, such as int, float, char, etc. It defines memory usage and operations. (30)

 

5.  What is type conversion in C++?

Type conversion in C++ is the process of converting one data type to another, like int to float. It can be done automatically or explicitly by the programmer. (30)

 

6.  What is type casting in C++?

Type casting in C++ is a method used to convert a variable from one data type to another explicitly using cast operators like static_cast or traditional C-style casting. (30)

 

7.  What is a variable in C++?

A variable in C++ is a named storage location used to hold a value. Its type determines the size and

layout of the memory and what operations can be performed. (30)

 

8. What is a constant variable in C++?

A constant variable in C++ is a variable whose value cannot be changed once initialized. It is declared using the const keyword to prevent modification during execution. (30)

 

9.     What is the scope of a variable in C++?

Scope of a variable in C++ refers to the part of the program where the variable is accessible. It can be local, global, block-level, or function-level depending on declaration. (30)

 

10.  How do you use the continue statement in a loop in C++?

The continue statement in C++ skips the current iteration of a loop and jumps to the next iteration, continuing the loop execution without executing the remaining statements. (30)

 

11.  What are jump statements in C++?

Jump statements in C++ are used to transfer control to another part of the program. They include break, continue, return, and goto for changing flow unexpectedly. (30)

 

12.  What is the purpose of the break statement in C++?

The break statement in C++ is used to exit from loops or switch statements prematurely. When encountered, it immediately terminates the loop or case block. (30)

 

13.  How do you use the continue statement in a loop in C++?

The continue statement is placed inside a loop to skip remaining statements for the current iteration and jump to the beginning of the next loop cycle automatically. (30)

 

14.  What is the syntax of the return statement in C++?

The syntax of the return statement in C++ is: return expression;. It is used to return a value from a function and exit from the function body. (30)

 

15.  How do you use the goto statement in C++?

The goto statement in C++ is used to transfer control to a labeled statement. It is declared using goto label; and then the label is defined with label:. (30)



C++ short answer

Chapter 2

 

 

16.  Explain bitwise operator and increment/decrement operator in C++.?

 

Bitwise operators perform operations on bits like AND (&), OR (|), XOR (^), etc. They are used for low-level operations. Increment (++) increases a value by 1, and decrement (--) decreases it by 1. These can be used in prefix or postfix form to change values quickly and are often used in loops or calculations. (60)

 

17.  What is the difference between unary and binary operators in C++?

 

Unary operators work with only one operand, like increment (++a) or decrement (--b). Binary operators need two operands, like a + b or x * y. Binary operators are more common in expressions, while unary operators are useful for modifying single variables in a concise way. Both types help in manipulating values efficiently in C++. (60)

 

18.  What is the difference between the assignment operator and the equality operator in C++?

 

The assignment operator (=) is used to assign a value to a variable, like a = 5. The equality operator (==) checks whether two values are equal, like a == b. Confusing them can lead to logical errors. Assignment changes value, while equality only compares values and returns a boolean result, true or false. (60)

 

19.  What is the difference between implicit and explicit type conversion in C++?

 

Implicit type conversion happens automatically by the compiler, like converting int to float. Explicit conversion is done manually by the programmer using casting, like (float) a. Implicit is safe but may lose precision. Explicit gives more control but should be used carefully. Both help in converting between data types when needed. (60)

 

20.  What is 1D and 2D array in C++?

 

A 1D array is a simple list of elements like int arr[5]. A 2D array is like a table with rows and columns, declared as int arr[3][3]. 1D is used for linear data, and 2D for matrix-type data. Access in 1D is with one index, in 2D with two indices. (60)

 

21.  What is the syntax of the if-else statement in C++?

 

The syntax of if-else is:

if(condition) {  

   // statements  

} else {  

   // other statements  

}  

It is used for decision making. If the condition is true, the first block runs; else the second block executes. It helps in controlling program flow based on conditions. (60)

 

22.  How do you mean by nested if statements in C++?

 

Nested if statements mean placing one if or if-else inside another. It allows checking multiple conditions step-by-step. For example, first check one condition, and if it’s true, check another condition inside. It's useful for multi-level decision-making where each condition depends on the previous one. Syntax becomes more complex with deeper nesting. (60)

 

23.  What is the syntax and working of the for loop in C++?

 

The syntax of a for loop is: for(initialization; condition; increment) {  

   // statements  

}  

It runs the block repeatedly until the condition becomes false. Initialization runs once, then condition is checked before each iteration, and increment updates the loop variable. It is useful when the number of iterations is known. (60)

 

24.  Difference between for and while loop in C++?

 

In a for loop, initialization, condition, and increment are written in a single line. It’s preferred when the number of iterations is known. In a while loop, only the condition is checked, and initialization/increment are written separately. It’s used when the number of iterations is not fixed beforehand. Both serve different use cases. (60)

 

25.  What is the do-while loop in C++?

 

The do-while loop in C++ first executes the loop body, then checks the condition. Syntax:

do {  

  // statements  

} while(condition);  

It ensures the loop runs at least once, even if the condition is false initially. Useful when execution must happen before checking the condition. (60)

 

26.  Difference between do-while and while loop in C++?

 

In a while loop, the condition is checked before executing the loop, so it may not run even once if the condition is false. In a do-while loop, the body runs first and then checks the condition, so it runs at least once. This is the key difference between both loops. (60)

 

27.  What is the purpose of the break statement in a loop or switch case in C++?

 

The break statement is used to immediately exit from a loop or a switch case. In loops, it stops further iterations when a certain condition is met. In switch, it prevents fall-through to the next case. It improves control over program flow and avoids unnecessary execution of code beyond certain points. (60)

 

28.  How do you use the switch case statement to handle multiple cases in C++?

 

The switch statement tests a variable against many constant values using case. Syntax: switch(expression) {     case value1: statements; break;     case value2: statements; break;     default: statements;  

}  

It’s used as an alternative to many if-else statements, making code cleaner and readable when dealing with multiple fixed options or choices. (60)

 

29.  What is a function in C++? Explain default argument concept.?

 

A function in C++ is a block of code that performs a task and can be called when needed. Default arguments allow us to call a function without passing all arguments. These default values are assigned in the function declaration. It increases flexibility and reduces overloads for functions where some parameters are optional. (60)

 

30. How do you declare a function in C++? Explain inline function concept.?

 

A function is declared as return_type function_name(parameters);. Inline functions are defined using inline keyword. These are small functions where the compiler replaces the function call with the actual code during compilation, reducing function call overhead. Inline functions are useful for small, frequently-used code blocks for faster execution. (60)

 

31. Explain various parts of the function in C++. Explain function overloading concept.

A function in C++ has several parts: return type, name, parameters, and body. Function overloading means having multiple functions with the same name but different parameters (type or number). The compiler differentiates them by arguments. It increases code readability and allows using the same function name for different tasks with different input types. (60)

 

32.  What is passing by value and call by reference?

In call by value, a copy of the actual parameter is passed to the function, so changes inside the function don’t affect the original variable. In call by reference, the function receives the memory address, so changes affect the original variable. Call by reference is useful when we want the function to modify actual data. (60)

 

33.  Difference between call by value and call by reference.

In call by value, changes made in the function don’t affect the original values. In call by reference, the original variables can be modified. Call by value is safer but less flexible, while call by reference allows more control but may cause unwanted changes. Use reference when changes are intended in the original data. (60)

 

34.  What is the lifetime of a variable and also explain use of scope resolution operator in C++? Lifetime means how long a variable exists in memory. Local variables exist inside a block and are destroyed when the block ends. Global variables exist throughout the program. The scope resolution operator :: is used to access global variables or class members when local names overlap. It helps in accessing correct variable versions. (60)

 

35.  How do you access members of the structure using (Dot) and (->) operator? When you have a structure variable, you access its members using the dot operator like structVar.member. If you have a pointer to the structure, use the arrow operator like ptr->member. The arrow operator is shorthand for (*ptr).member. These operators help access and manipulate data inside structures effectively and clearly. (60)

 

36.  What is the difference between a structure and a union in C++? Give an example of when you might use each.

In structures, all members have separate memory locations. In unions, all members share the same memory, and only one can hold a value at a time. Structures are used when storing multiple related values, while unions are used when working with different data types but only one is needed at a time. (60)

 

37.  What is the syntax for declaring a structure in C++? The syntax is:

struct StructName {     data_type member1;     data_type member2;  

};  

Then we can create a variable like StructName obj;. Structures group related variables under one name and are useful for organizing complex data types, like student info with name, roll number, and marks. (60)

 

38.  What is the syntax for declaring a union in C++? How do you access its members? The syntax is:

union UnionName {  

  int a;     float b;  

};  

You can access members using the dot operator like obj.a. In unions, only one member holds value at a time since all share the same memory. Useful when memory optimization is important and only one variable is needed at a time. (60)

 

39.  How do you initialize an array in C++? To initialize an array in C++, you can use:

int arr[5] = {1, 2, 3, 4, 5};  

You can also skip the size like int arr[] = {1, 2, 3}. Elements are stored in continuous memory. Uninitialized elements are set to 0 by default if partially initialized. Proper initialization prevents garbage values and helps in consistent data processing. (60)

 

40.  How do you access elements of an array in C++?

Elements of an array are accessed using index numbers, starting from 0. For example, arr[0] gives the first element, and arr[2] gives the third. Indexes must be within array size. You can also use loops to access all elements. Accessing with wrong index may cause undefined behavior, so bounds should always be checked. (60)

 

 


Post a Comment

0 Comments