Question Bank - OOP1 - Ch1-3
Question Bank - OOP1 - Ch1-3
1. What is a pointer in C?
A) A keyword used to define variables
B) A variable that stores the address of another variable
C) A function that points to another function
D) A data type in C
Answer: B) A variable that stores the address of another variable
3. Which of the following correctly assigns the address of variable a to pointer ptr?
A) ptr = &a;
B) ptr = *a;
C) ptr = address(a);
D) ptr = a;
Answer: A) ptr = &a;
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("%d", *ptr);
return 0;
}
A) 10
B) Address of a
C) Garbage value
D) Compilation error
Answer: A) 10
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40};
int *ptr = arr;
printf("%d", *(ptr + 2));
return 0;
}
A) 10
B) 20
C) 30
D) 40
Answer: C) 30
#include <stdio.h>
int main() {
int a = 5, b = 10, *ptr1 = &a, *ptr2 = &b;
*ptr1 = *ptr2;
printf("%d %d", a, b);
return 0;
}
A) 10 10
B) 5 10
C) 10 5
D) 5 5
Answer: A) 10 10
9. Which of the following is the correct way to dynamically allocate memory for an
integer using pointers?
A) int *ptr = malloc(sizeof(int));
B) int ptr = (int)malloc(sizeof(int));
C) int ptr = malloc(sizeof(int));
D) int *ptr = (int)malloc(sizeof());
Answer: A) int *ptr = malloc(sizeof(int));
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("%p", ptr);
return 0;
}
A) Value of a
B) Address of a
C) 10
D) Compilation error
Answer: B) Address of a
#include <stdio.h>
int main() {
int a = 5, b = 15, *p1 = &a, *p2 = &b;
p1 = p2;
printf("%d", *p1);
return 0;
}
A) 5
B) 15
C) Address of a
D) Compilation error
Answer: B) 15
#include <stdio.h>
void change(int *p) {
*p = 20;
}
int main() {
int a = 10;
change(&a);
printf("%d", a);
return 0;
}
A) 10
B) 20
C) Compilation error
D) Undefined behavior
Answer: B) 20
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr + 3));
return 0;
}
A) 1
B) 2
C) 3
D) 4
Answer: D) 4
#include <stdio.h>
int main() {
int a = 5, *p = &a;
*p += 1;
printf("%d", a);
return 0;
}
A) 5
B) 6
C) Address of a
D) Compilation error
Answer: B) 6
24. What is the correct way to declare a pointer to a function that returns an int and
takes no arguments?
A) int *func();
B) int (*funcPtr)();
C) int &funcPtr();
D) pointer int func();
7. What is the correct syntax for dynamically allocating memory for an integer in
C++?
A) int *ptr = new int;
B) int ptr = new int;
C) int *ptr = malloc(sizeof(int));
D) int ptr = (int)malloc(sizeof(int));
Answer: A) int *ptr = new int;
11. What is the correct way to allocate memory for an array of 5 integers in C?
A) int *ptr = malloc(5 * sizeof(int));
B) int *ptr = calloc(5, sizeof(int));
C) Both A and B
D) int *ptr = new int[5];
Answer: C) Both A and B
15. Which is the correct way to release memory allocated using calloc() in C?
A) delete ptr;
B) delete[] ptr;
C) free(ptr);
D) clear(ptr);
Answer: C) free(ptr);
20. Which of the following is true about dynamic memory allocation in C++?
A) new and delete should be used together
B) malloc() and free() should be used together
C) new can be used with free()
D) malloc() can be used with delete
Answer: A) new and delete should be used together
18. What is the correct way to check if a file has reached the end?
A) feof(fp)
B) eof(fp)
C) checkeof(fp)
D) endoffile(fp)
Answer: A) feof(fp)
20. What happens if you try to open a file in "r" mode, but the file does not exist?
A) The file is created
B) The program terminates with an error
C) fopen() returns NULL
D) A new file is opened in write mode
Answer: C) fopen() returns NULL
CH:02
1. What is Object-Oriented Programming (OOP)?
A) A programming paradigm based on functions
B) A programming paradigm based on objects and classes
C) A programming language
D) A type of operating system
Answer: B) A programming paradigm based on objects and classes
Principles of OOP
8. Which OOP principle allows one class to acquire properties of another?
A) Encapsulation
B) Polymorphism
C) Inheritance
D) Abstraction
Answer: C) Inheritance
10. Which OOP principle allows objects of different classes to be treated as objects
of a common superclass?
A) Encapsulation
B) Polymorphism
C) Inheritance
D) Abstraction
Answer: B) Polymorphism
11. Which OOP principle is used to hide complexity by showing only relevant
details?
A) Inheritance
B) Encapsulation
C) Abstraction
D) Polymorphism
Answer: C) Abstraction
Benefits of OOP
13. Which of the following is a key advantage of OOP?
A) Code reusability
B) Code redundancy
C) Difficulty in debugging
D) Less security
Answer: A) Code reusability
Applications of OOP
16. Which of the following applications is best suited for OOP?
A) System programming
B) Real-time simulations
C) Data processing
D) Scripting languages
Answer: B) Real-time simulations
17. Which of the following domains benefit the most from OOP?
A) Web development
B) Game development
C) Database management
D) All of the above
Answer: D) All of the above
CH:03
1. What is the correct entry point of a C++ program?
A) start()
B) main()
C) program()
D) entry()
Answer: B) main()
int x = 10;
cout << x;
A) 10
B) x
C) Error
D) 0
Answer: A) 10
Operators
10. What type of operator is + in C++?
A) Unary operator
B) Binary operator
C) Ternary operator
D) Bitwise operator
Answer: B) Binary operator
Typecasting
13. What is typecasting in C++?
A) Converting one data type to another
B) Assigning values to constants
C) Creating new data types
D) Overloading functions
Answer: A) Converting one data type to another
Control Structures
16. Which of the following is NOT a control structure in C++?
A) Loop
B) Condition
C) Function
D) Switch
Answer: C) Function
if (5 > 3)
cout << "Hello";
else
cout << "World";
A) Hello
B) World
C) Error
D) No output
Answer: A) Hello
int i = 1;
while (i < 4) {
cout << i;
i++;
}
A) 123
B) 0123
C) 4321
D) 1234
Answer: A) 123