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

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

  3. Can const variables be modified after they are initialized? a) Yes b) No Answer: b

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

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

  6. Is the const keyword required when passing a variable by reference in C++? a) Yes b) No Answer: a

  7. Can a member function of a C++ class be declared as const? a) Yes b) No Answer: a

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

  9. Can a const variable be initialized with a value at runtime in C++? a) Yes b) No Answer: b

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

  2. Why is using const variables beneficial in a program? Answer: Using const variables can help prevent bugs and improve program stability.

  3. Can const variables be modified after they are initialized? Answer: No, const variables cannot be modified after they are initialized.

  4. What happens if you try to modify a const variable in C++? Answer: The compiler generates an error.

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

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

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

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

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

  10. Is the const keyword used in other programming languages besides C++? Answer: Yes, the const keyword is used in many programming languages besides C++.

In programming, the const keyword is used to declare variables that cannot be modified after they are initialized. This helps prevent bugs and improves program stability by ensuring that important variables are not accidentally modified during program execution. In C++, const variables can be used in a variety of ways. For example, they can be used to represent mathematical constants or program-specific values that should not be changed. They can also be used to make functions and methods safer by ensuring that inputs are not modified. In addition to declaring const variables, the const keyword can also be used in other contexts. For example, when passing a variable by reference in C++, the const keyword can be used to ensure that the variable is not modified by the function being called. Similarly, when declaring a member function of a class, the const keyword can be used to indicate that the function does not modify the state of the class instance it is called on. The const keyword can also be used in pointer declarations to ensure that the pointer cannot be used to modify the value it points to. 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. Overall, the const keyword is a powerful tool that can help improve program stability and prevent bugs by ensuring that important variables are not accidentally modified.