0% found this document useful (0 votes)
12 views14 pages

Question Bank - OOP1 - Ch1-3

The document contains a series of questions and answers related to pointers, memory allocation, and file handling in C and C++. It covers topics such as pointer declaration, pointer arithmetic, dynamic memory allocation, and file operations, providing correct answers to each question. The content serves as a study guide for understanding key concepts in C programming.

Uploaded by

senona6037
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

Question Bank - OOP1 - Ch1-3

The document contains a series of questions and answers related to pointers, memory allocation, and file handling in C and C++. It covers topics such as pointer declaration, pointer arithmetic, dynamic memory allocation, and file operations, providing correct answers to each question. The content serves as a study guide for understanding key concepts in C programming.

Uploaded by

senona6037
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Ch:01

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

2. What is the correct syntax to declare a pointer to an integer?


A) int ptr;
B) int *ptr;
C) int &ptr;
D) pointer int ptr;
Answer: B) int *ptr;

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;

4. What will be the output of the following code?

#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

5. What does the NULL pointer in C signify?


A) It points to an invalid memory location
B) It holds the value 0 and does not point to any valid memory location
C) It is the address of the first variable in a program
D) It is used to allocate memory dynamically
Answer: B) It holds the value 0 and does not point to any valid memory location

6. What happens when you increment a pointer?


A) It moves to the next memory location of the same data type
B) It moves to the previous memory location
C) It changes the value stored at that memory location
D) It gives a compilation error
Answer: A) It moves to the next memory location of the same data type

7. What will be the output of the following code?

#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

8. What will be the output of the following code?

#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));

10. What is a dangling pointer?


A) A pointer pointing to a memory location that has been freed
B) A pointer pointing to an array
C) A pointer initialized to NULL
D) A pointer that stores the address of another pointer
Answer: A) A pointer pointing to a memory location that has been freed

1. Which of the following is true about pointer arithmetic?


A) Only addition and subtraction operations can be performed on pointers
B) All arithmetic operations can be performed on pointers
C) Pointer arithmetic is not allowed in C
D) Only increment and decrement operations are allowed
Answer: A) Only addition and subtraction operations can be performed on pointers

12. What will be the output of the following code?

#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

13. What will happen if a pointer is not initialized?


A) It will point to NULL
B) It will cause a compilation error
C) It will contain a garbage address
D) It will always point to the first variable in the program
Answer: C) It will contain a garbage address

14. Which of the following is NOT a valid pointer operation?


A) ptr1 + ptr2
B) ptr + 1
C) ptr - ptr2
D) ptr++
Answer: A) ptr1 + ptr2

15. What will be the output of the following code?

#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

16. What will be the output of the following code?

#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

17. Which function is used to allocate memory dynamically in C?


A) alloc()
B) malloc()
C) new()
D) allocate()
Answer: B) malloc()

18. What is the size of a pointer variable in a 64-bit system?


A) 2 bytes
B) 4 bytes
C) 8 bytes
D) Depends on the data type it points to
Answer: C) 8 bytes

19. What will be the output of the following code?

#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

20. Which of the following is true about an array and a pointer in C?


A) An array is a pointer
B) A pointer is an array
C) The name of an array acts as a pointer to the first element
D) A pointer can only store the address of another pointer
Answer: C) The name of an array acts as a pointer to the first element

21. What happens when free() is called on a pointer?


A) The pointer is deleted from memory
B) The memory block pointed to by the pointer is deallocated
C) The pointer is set to NULL
D) The program terminates
Answer: B) The memory block pointed to by the pointer is deallocated

22. What is a void pointer?


A) A pointer that stores the address of an integer
B) A pointer that can point to any data type
C) A pointer that points to NULL
D) A pointer that only stores memory addresses
Answer: B) A pointer that can point to any data type

23. What will be the output of the following code?

#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();

Answer: B) int (*funcPtr)();

25. What is pointer decay in C?


A) When a pointer loses its value
B) When an array name is treated as a pointer to its first element
C) When a pointer is automatically set to NULL
D) When a pointer loses memory allocation
Answer: B) When an array name is treated as a pointer to its first element

1. Which function is used to allocate memory dynamically in C?


A) malloc()
B) new
C) allocate()
D) memory_alloc()
Answer: A) malloc()

2. Which function is used to allocate memory dynamically in C++?


A) malloc()
B) alloc()
C) new
D) allocate()
Answer: C) new

3. Which function in C is used to allocate memory for an array and initializes it to


zero?
A) malloc()
B) calloc()
C) realloc()
D) free()
Answer: B) calloc()

4. What is the return type of malloc() and calloc() in C?


A) void*
B) int*
C) char*
D) float*
Answer: A) void*

5. How do you deallocate memory allocated by malloc() in C?


A) delete
B) free()
C) remove()
D) clear()
Answer: B) free()

6. What happens if memory allocation using malloc() fails in C?


A) The program crashes
B) A segmentation fault occurs
C) malloc() returns NULL
D) The system restarts
Answer: C) malloc() returns NULL

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;

8. What is the correct way to free dynamically allocated memory in C++?


A) delete ptr;
B) free(ptr);
C) dealloc(ptr);
D) remove(ptr);
Answer: A) delete ptr;

9. How do you deallocate an array allocated using new in C++?


A) delete ptr;
B) delete[] ptr;
C) free(ptr);
D) clear(ptr);
Answer: B) delete[] ptr;

10. Which function is used to reallocate memory in C?


A) malloc()
B) calloc()
C) realloc()
D) delete
Answer: C) realloc()

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

12. What happens if you delete a pointer twice in C++?


A) It results in a segmentation fault (undefined behavior)
B) It frees the memory again
C) It returns NULL
D) It prevents memory leaks
Answer: A) It results in a segmentation fault (undefined behavior)

13. Which of the following statements is true regarding realloc() in C?


A) It reallocates memory without changing the original data
B) It initializes newly allocated memory to zero
C) It always increases memory size
D) It frees previously allocated memory
Answer: A) It reallocates memory without changing the original data

14. What happens if new fails to allocate memory in C++?


A) Returns NULL
B) Throws std::bad_alloc exception
C) Terminates the program
D) Calls malloc() internally
Answer: B) Throws std::bad_alloc exception

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);

16. What is the difference between malloc() and calloc()?


A) malloc() initializes allocated memory to zero, but calloc() does not
B) calloc() initializes allocated memory to zero, but malloc() does not
C) Both allocate and initialize memory to zero
D) malloc() is used for arrays, while calloc() is for single variables
Answer: B) calloc() initializes allocated memory to zero, but malloc() does not

17. What does delete do in C++?


A) It deallocates memory allocated using malloc()
B) It deallocates memory allocated using new
C) It reallocates memory
D) It initializes memory to zero
Answer: B) It deallocates memory allocated using new

18. What is a memory leak?


A) When memory is allocated but not properly deallocated
B) When memory is freed before being used
C) When a pointer stores NULL
D) When the program crashes due to an invalid pointer
Answer: A) When memory is allocated but not properly deallocated

19. What happens when free() is called on a NULL pointer?


A) It crashes the program
B) It does nothing
C) It results in a segmentation fault
D) It deallocates some memory
Answer: B) It does nothing

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

1. Which header file is required for file handling in C?


A) <file.h>
B) <fmanage.h>
C) <stdio.h>
D) <stdlib.h>
Answer: C) <stdio.h>

2. Which function is used to open a file in C?


A) openfile()
B) fopen()
C) fileopen()
D) open()
Answer: B) fopen()

3. What does the mode "r" do when opening a file?


A) Opens the file for reading
B) Opens the file for writing
C) Opens the file for appending
D) Opens the file in binary mode
Answer: A) Opens the file for reading

4. What does the mode "w" do when opening a file?


A) Opens the file for writing (overwrites existing content)
B) Opens the file for reading
C) Opens the file for writing without overwriting
D) Opens the file in read and write mode
Answer: A) Opens the file for writing (overwrites existing content)

5. What does the "a" mode do when opening a file?


A) Appends data to the file
B) Opens the file for reading
C) Deletes all content in the file
D) Opens the file in read and write mode
Answer: A) Appends data to the file

6. What is the return type of fopen()?


A) FILE*
B) int
C) char*
D) void
Answer: A) FILE*

7. What will fopen() return if the file cannot be opened?


A) NULL
B) 0
C) 1
D) EOF
Answer: A) NULL

8. Which function is used to close a file in C?


A) fclose()
B) closefile()
C) fileclose()
D) endfile()
Answer: A) fclose()

9. Which function is used to read a single character from a file?


A) freadchar()
B) fgetc()
C) getchar()
D) readchar()
Answer: B) fgetc()

10. Which function is used to write a single character to a file?


A) putchar()
B) writechar()
C) fputc()
D) fwrite()
Answer: C) fputc()

11. What is the function used to read a string from a file?


A) fgetstr()
B) fgets()
C) getstring()
D) readline()
Answer: B) fgets()

12. What is the function used to write a string to a file?


A) fputstr()
B) fputs()
C) putstring()
D) writestring()
Answer: B) fputs()

13. Which function is used to read a block of data from a file?


A) fread()
B) readfile()
C) fileread()
D) readblock()
Answer: A) fread()

14. Which function is used to write a block of data to a file?


A) fwrite()
B) writefile()
C) filewrite()
D) writeblock()
Answer: A) fwrite()

15. What will fseek(fp, 0, SEEK_SET); do?


A) Move the file pointer to the beginning of the file
B) Move the file pointer to the end of the file
C) Move the file pointer one character forward
D) Move the file pointer to the middle of the file
Answer: A) Move the file pointer to the beginning of the file

16. What will ftell(fp); return?


A) The size of the file
B) The current position of the file pointer
C) The last character in the file
D) The number of lines in the file
Answer: B) The current position of the file pointer

17. What is the purpose of rewind(fp);?


A) Moves the file pointer to the beginning of the file
B) Moves the file pointer to the end of the file
C) Deletes the file content
D) Moves the file pointer to the previous line
Answer: A) Moves the file pointer to the beginning of the file

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)

19. Which function is used to remove a file in C?


A) delete()
B) remove()
C) unlink()
D) erase()
Answer: B) remove()

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

2. Which of the following is not a feature of OOP?


A) Encapsulation
B) Polymorphism
C) Inheritance
D) Sequential execution
Answer: D) Sequential execution

3. Which of the following languages does NOT support OOP?


A) C++
B) Java
C) C
D) Python
Answer: C) C

4. Which of the following is an example of an object in OOP?


A) A function
B) A class
C) A variable
D) A real-world entity like a Car
Answer: D) A real-world entity like a Car

Procedural vs. Object-Oriented Programming


5. Which of the following best describes procedural programming?
A) Uses functions and procedures to structure programs
B) Focuses on objects and data encapsulation
C) Uses inheritance to reuse code
D) Does not use any variables
Answer: A) Uses functions and procedures to structure programs

6. Which of the following is NOT a characteristic of procedural programming?


A) Top-down approach
B) Emphasis on functions
C) Uses classes and objects
D) Code reusability is lower than OOP
Answer: C) Uses classes and objects
7. Which of the following is an advantage of OOP over procedural programming?
A) More modular and reusable code
B) Less memory usage
C) No need for functions
D) More difficult debugging process
Answer: A) More modular and reusable code

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

9. What is the primary benefit of encapsulation?


A) Hides internal details of an object
B) Allows multiple functions with the same name
C) Enables object inheritance
D) Makes code execution faster
Answer: A) Hides internal details of an object

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

12. What is function overloading an example of?


A) Encapsulation
B) Polymorphism
C) Inheritance
D) Abstraction
Answer: B) Polymorphism

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

14. Which OOP feature helps to reduce code duplication?


A) Encapsulation
B) Inheritance
C) Polymorphism
D) Abstraction
Answer: B) Inheritance

15. What is the benefit of data abstraction in OOP?


A) Hides unnecessary implementation details
B) Increases code complexity
C) Reduces security
D) Makes debugging harder
Answer: A) Hides unnecessary implementation details

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

18. How does OOP help in large-scale software development?


A) By reducing code complexity
B) By increasing code reusability
C) By providing modularity
D) All of the above
Answer: D) All of the above

19. Which of the following best describes an OOP-based software design?


A) Code is divided into functions
B) Code is structured using objects and classes
C) Code is written only using loops and conditions
D) Code does not use functions
Answer: B) Code is structured using objects and classes

20. Which of the following is an example of an OOP-based software?


A) Microsoft Word
B) Turbo C Compiler
C) Unix Shell
D) Assembly Language
Answer: A) Microsoft Word

CH:03
1. What is the correct entry point of a C++ program?
A) start()
B) main()
C) program()
D) entry()
Answer: B) main()

2. Which of the following is the correct structure of a C++ program?


A) Header files → Main function → Other functions
B) Main function → Header files → Other functions
C) Other functions → Header files → Main function
D) Header files → Other functions → Main function
Answer: A) Header files → Main function → Other functions

3. What is the correct syntax to include a header file in C++?


A) #include <iostream>
B) include <iostream>
C) #include iostream
D) <include iostream>
Answer: A) #include <iostream>
Namespace
4. What is the purpose of a namespace in C++?
A) To define multiple main() functions
B) To avoid name conflicts
C) To execute programs faster
D) To allow direct memory access
Answer: B) To avoid name conflicts

5. What is the correct way to use the standard namespace in C++?


A) namespace std;
B) using std;
C) using namespace std;
D) namespace using std;
Answer: C) using namespace std;

Identifiers & Variables


6. Which of the following is a valid identifier in C++?
A) int
B) 123variable
C) _myVar
D) #define
Answer: C) _myVar

7. What will be the output of the following C++ code?

int x = 10;
cout << x;
A) 10
B) x
C) Error
D) 0
Answer: A) 10

Constants & Enums


8. How do you define a constant variable in C++?
A) constant int x = 5;
B) const int x = 5;
C) define x 5;
D) static int x = 5;
Answer: B) const int x = 5;

9. What is an enum in C++?


A) A user-defined data type with fixed values
B) A loop control structure
C) A type of class
D) A function
Answer: A) A user-defined data type with fixed values

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

11. What is the result of the expression 10 % 3?


A) 3
B) 1
C) 10
D) 0
Answer: B) 1
12. What does the && operator do in C++?
A) Bitwise AND
B) Logical AND
C) Bitwise OR
D) Logical OR
Answer: B) Logical AND

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

14. Which of the following is an example of implicit typecasting?


A) int x = 10.5;
B) double y = (double)10;
C) float z = static_cast<float>(10);
D) char c = (char)97;
Answer: A) int x = 10.5;

15. What is the correct syntax for explicit typecasting in C++?


A) type_cast(value);
B) (type)value;
C) convert(value, type);
D) type_cast::value;
Answer: B) (type)value;

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

17. What will be the output of the following code?

if (5 > 3)
cout << "Hello";
else
cout << "World";
A) Hello
B) World
C) Error
D) No output
Answer: A) Hello

18. Which of the following is NOT a loop structure in C++?


A) for
B) while
C) do-while
D) switch
Answer: D) switch

19. How many times will the following loop execute?

for (int i = 0; i < 5; i++)


cout << i;
A) 4
B) 5
C) Infinite
D) 0
Answer: B) 5

20. What will be the output of the following code?

int i = 1;
while (i < 4) {
cout << i;
i++;
}
A) 123
B) 0123
C) 4321
D) 1234
Answer: A) 123

You might also like