Skip to main content

Introduction to Programming MCQs with Answers

Practice important Introduction to Programming MCQs with answers and explanations.

Chapter: 38
Q601:
How do you define a parameter-less user-defined manipulator?
A. ostream& manipulator_name (ostream& os)
B. ostream manipulator_name (ostream& os)
C. ostream& manipulator_name (ostream os)
D. ostream manipulator_name (ostream os)
Correct Answer: A
Chapter: 38
Q602:
What does the endl manipulator do?
A. Inserts a tab character
B. Inserts a newline and flushes the buffer
C. Adds a beep sound
D. Adds a space between numbers
Correct Answer: B
Chapter: 38
Q603:
In the provided code, what does the tab manipulator do?
A. Inserts a newline
B. Inserts a tab character
C. Inserts a star
D. Inserts a space between elements
Correct Answer: B
Chapter: 38
Q604:
Which function is used to create a static variable within a function in C++?
A. static
B. global
C. local
D. auto
Correct Answer: A
Chapter: 38
Q605:
What is the main purpose of using static variables inside functions?
A. To maintain the variable's value between function calls
B. To create variables that are only accessible in main
C. To ensure variables are always initialized
D. To allocate memory dynamically
Correct Answer: A
Chapter: 38
Q606:
What will happen if you declare a static variable inside the main function?
A. It will be initialized only once during the program's execution
B. It will be visible only in the main function
C. It will be accessible only inside loops
D. It will be destroyed after the main function exits
Correct Answer: B
Chapter: 38
Q607:
Which keyword is used to declare a variable that persists between function calls?
A. const
B. auto
C. static
D. extern
Correct Answer: C
Chapter: 38
Q608:
In the context of user-defined manipulators, what does the flush function do?
A. Clears the buffer
B. Inserts a tab character
C. Inserts a newline
D. Ends the current line
Correct Answer: A
Chapter: 38
Q609:
What is the correct way to overload the stream insertion operator (<<) for user-defined manipulators?
A. ostream& operator << (ostream& os, MyClass& obj)
B. ostream& operator << (MyClass& obj, ostream& os)
C. ostream& operator << (ostream& os, MyClass obj)
D. ostream& operator << (MyClass obj, ostream& os)
Correct Answer: A
Chapter: 38
Q610:
What is the primary purpose of the static keyword in a function?
A. To create a global variable
B. To make a variable accessible throughout the file
C. To maintain a variable's state across function calls
D. To allocate dynamic memory
Correct Answer: C
Chapter: 38
Q611:
What happens to a static variable's value when a function is called multiple times?
A. It is reset to its initial value each time
B. It is incremented and preserved across calls
C. It is destroyed and recreated on each call
D. It is not visible to the function
Correct Answer: B
Chapter: 38
Q612:
In the given code, what will be the output of the staticVarFun() function call inside a loop?
A. The value of i will always be 1
B. The value of i will increment with each call
C. The value of i will be random
D. The function will not print anything
Correct Answer: B
Chapter: 38
Q613:
What will be the output of the nonstaticVarFun() function when called in a loop?
A. The value of i will increment with each call
B. The value of i will always be 1
C. The value of i will be random
D. The function will not print anything
Correct Answer: B
Chapter: 38
Q614:
How does the destruction of static objects in functions differ from local objects?
A. Static objects are destroyed when the function exits
B. Static objects are destroyed at the end of the program
C. Static objects are not destroyed
D. Static objects are destroyed immediately
Correct Answer: B
Chapter: 38
Q615:
What happens if you use the static keyword with class data members?
A. Each object has its own copy of the static member
B. Only one copy of the static member exists for the class
C. Static members are not accessible from objects
D. Each static member is initialized per object
Correct Answer: B
Chapter: 38
Q616:
How should you initialize a static data member of a class?
A. Inside the constructor of the class
B. Inside the destructor of the class
C. Outside the class definition using the scope resolution operator
D. Inside each object's constructor
Correct Answer: C
Chapter: 38
Q617:
When should you use the class name to access a static member?
A. When you need to refer to the member across all objects
B. When the member is private
C. When you want to initialize it
D. When the member is not static
Correct Answer: A
Chapter: 38
Q618:
In the provided program, what is the order of destruction of objects?
A. Static objects, then local objects, then global objects
B. Global objects, then static objects, then local objects
C. Local objects, then static objects, then global objects
D. Global objects, then local objects, then static objects
Correct Answer: B
Chapter: 38
Q619:
What is a common use case for static data members in a class?
A. To store information specific to each object
B. To maintain a count of objects created
C. To store temporary data
D. To manage dynamic memory allocation
Correct Answer: B
Chapter: 39
Q620:
What does a pointer in C++ store?
A. The value of a variable
B. The memory address of a variable
C. The size of a variable
D. The type of a variable
Correct Answer: B

Test Your Knowledge

Take a timed quiz on Introduction to Programming

🚀 Start Quiz Now