19 Lecture
CS301
Midterm & Final Term Short Notes
Usage of const keyword
The const keyword in programming languages is used to declare variables that cannot be modified once they are initialized. This allows for the creation of immutable variables, which can help prevent bugs and improve program stability. The const
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
What does the const keyword do in C++? a) Declares a variable that cannot be modified b) Declares a variable that can only be modified in specific contexts c) Declares a variable that is used for debugging purposes Answer: a
Which of the following is a benefit of using const variables? a) Improved program performance b) Increased memory usage c) Reduced bugs and improved program stability Answer: c
Can const variables be modified after they are initialized? a) Yes b) No Answer: b
What happens if you try to modify a const variable in C++? a) The program crashes b) The compiler generates an error c) The modification is allowed Answer: b
Which of the following types of variables is typically declared as const in C++? a) Variables that are used for input/output b) Global variables c) Variables that are used as constants in the program Answer: c
Is the const keyword required when passing a variable by reference in C++? a) Yes b) No Answer: a
Can a member function of a C++ class be declared as const? a) Yes b) No Answer: a
Which of the following is an example of a const pointer in C++? a) int* const ptr; b) const int* ptr; c) const int* const ptr; Answer: a
Can a const variable be initialized with a value at runtime in C++? a) Yes b) No Answer: b
Is the const keyword used in other programming languages besides C++? a) Yes b) No Answer: a
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is the purpose of the const keyword in programming? Answer: The const keyword is used to declare variables that cannot be modified once they are initialized.
Why is using const variables beneficial in a program? Answer: Using const variables can help prevent bugs and improve program stability.
Can const variables be modified after they are initialized? Answer: No, const variables cannot be modified after they are initialized.
What happens if you try to modify a const variable in C++? Answer: The compiler generates an error.
Is the const keyword required when passing a variable by reference in C++? Answer: Yes, the const keyword is required when passing a variable by reference in C++.
Can a member function of a C++ class be declared as const? Answer: Yes, a member function of a C++ class can be declared as const.
What is the difference between a const pointer and a pointer to a const variable in C++? Answer: A const pointer is a pointer that cannot be modified to point to a different memory address, while a pointer to a const variable is a pointer that cannot be used to modify the value of the variable it points to.
Can a const variable be initialized with a value at runtime in C++? Answer: No, a const variable must be initialized with a value at compile-time in C++.
What are some examples of variables that are commonly declared as const in C++? Answer: Constants used in mathematical calculations, physical constants, and program-specific constants are all examples of variables that are commonly declared as const in C++.
Is the const keyword used in other programming languages besides C++? Answer: Yes, the const keyword is used in many programming languages besides C++.