A function in programming is a block of code that performs a specific task. It can take inputs (parameters) and return an output, allowing you to reuse the code multiple times in different parts of your program.
Which of the following is a method to prevent memory leaks in C?
A Use free()
B Avoid pointers
C Use sizeof()
D Use malloc()
In C, memory allocated dynamically using malloc() must be deallocated manually using free() to prevent memory leaks. Failing to do so can cause the program to consume more memory than required, leading to performance issues.
In Java, which of these is the correct way to declare a class?
A class ClassName {}
B object ClassName {}
C define ClassName {}
D class ClassName[] {}
In Java, a class is declared using the class keyword followed by the class name and curly braces {}. This defines a new class where you can declare variables and methods.
What does recursion in programming mean?
A Calling a function from within itself
B Reusing code
C Returning from a function
D Repeating a task
Recursion occurs when a function calls itself within its own body. This allows solving problems that can be broken down into smaller, similar problems. It continues until a base case is met.
Which of these is used to define a constant in C?
A final
B define
C immutable
D const
In C, the const keyword is used to define constants, making their values immutable during the program’s execution. This ensures that the value cannot be changed after it has been assigned.
How do you declare a class variable in Python?
A def name
B self.name
C var name
D class name
In Python, class variables are typically defined inside methods, using the self keyword. The self keyword refers to the instance of the class, and variables are bound to the instance, not to the class itself.
Which of the following is a characteristic of object-oriented programming?
A Functions
B Variables
C Loops
D Classes
Object-oriented programming (OOP) is centered around classes and objects. A class serves as a blueprint to create objects, and it encapsulates data and methods that operate on the data.
In Python, how is memory managed automatically?
A Using manual pointers
B Through garbage collection
C With free()
D With malloc()
Python uses garbage collection to automatically manage memory. When an object is no longer referenced, Python’s garbage collector automatically frees the memory used by the object, preventing memory leaks.
What is the purpose of the new keyword in Java?
A Allocate memory for objects
B Define constants
C Assign values
D Declare a variable
The new keyword in Java is used to allocate memory for new objects. It calls the constructor to initialize the new object, which is then ready to be used in the program.
Which of the following is a function used to get the length of a string in C?
A strsize()
B length()
C size()
D strlen()
In C, the strlen() function is used to determine the length of a string. It returns the number of characters in the string, excluding the null terminator (‘\0’).
What does this keyword refer to in Java?
A Parent class
B Static method
C Current object
D Previous object
In Java, the this keyword refers to the current instance of the class. It is used to refer to instance variables and methods within the class, distinguishing them from local variables or parameters with the same name.
Which of the following is an example of dynamic memory allocation in C?
A double x = 5
B int* arr = malloc(10 * sizeof(int))
C int arr[10];
D char str[20];
Dynamic memory allocation in C is done using functions like malloc() to allocate memory during runtime. The example malloc(10 * sizeof(int)) allocates memory for an array of 10 integers dynamically.
What does the super() function do in Java?
A Ends the program
B Creates a new instance
C Defines a class variable
D Calls a parent class constructor
In Java, super() is used to call a constructor from the parent class. This allows a subclass to initialize the parent class’s state before adding its own functionality.
Which operator is used for logical OR in C?
A ==
B &&
C |
D &
In C, the | operator is used for logical OR. It returns true (1) if at least one of the operands is true. The && operator is used for logical AND, which requires both operands to be true.
What is the role of the main() function in C?
A Handle errors
B Start program execution
C Declare variables
D Initialize memory
In C, the main() function is the entry point of the program. It is where execution begins, and the operating system calls it to start the program. It usually returns an integer value to indicate program success or failure.