In C, which function is used to handle memory allocation dynamically?
A malloc()
B free()
C calloc()
D sizeof()
In C, the malloc() function is used to allocate memory dynamically during runtime. It allocates a specified number of bytes and returns a pointer to the beginning of the allocated block of memory.
Which of these is an example of a data structure in programming?
A function
B method
C array
D loop
An array is a data structure that stores multiple values of the same data type. It allows for efficient access to elements using an index. Arrays are fundamental structures in many programming languages, including C, Python, and Java.
What does exception handling allow you to do in a program?
A Handle errors during runtime
B Perform conditional checks
C Loop through values
D Allocate memory dynamically
Exception handling in programming enables the handling of runtime errors. Using constructs like try-except in Python or try-catch in Java, you can gracefully manage errors without crashing the program.
How do you define a class in Java?
A class ClassName {}
B def ClassName {}
C class(ClassName)
D new ClassName()
In Java, a class is defined using the class keyword followed by the class name and curly braces. The class can contain attributes and methods that define the behavior of the objects created from it.
In Python, what does the finally block do in exception handling?
A Catches errors
B Defines exceptions
C Ensures code execution
D Raises an error
The finally block in Python ensures that code is executed no matter what, even if an exception occurs. It is used for cleanup activities, such as closing files or releasing resources, after handling exceptions.
In C, how do you allocate memory for an array of 10 integers dynamically?
A int* arr = malloc(10);
B int arr[10];
C int* arr = calloc(10);
D int* arr = malloc(10 * sizeof(int));
In C, dynamic memory for an array can be allocated using malloc(). To allocate space for 10 integers, malloc(10 * sizeof(int)) ensures the correct amount of memory is allocated for an array of integers.
In Java, what does the super() keyword do?
A Accesses class variables
B Calls a superclass constructor
C Creates a new object
D Returns a value
In Java, super() is used to call a constructor from the superclass. It is often used in the subclass constructor to initialize the parent class before adding subclass-specific behavior.
What is the output of the following Python code: print(“123” + “456”)?
A 579
B None
C 123456
D TypeError
In Python, when two strings are concatenated using the + operator, they combine. Therefore, “123” + “456” results in the string “123456”.
In C, what is returned by the sizeof() function?
A Size of data type
B Memory address
C Length of array
D Value of variable
The sizeof() function in C returns the size (in bytes) of the specified data type or variable. It is useful for determining the memory requirements of a given type or array.
What is the main advantage of using dynamic memory allocation in C?
A Faster execution
B Requires no cleanup
C No need to define array size
D Reduces code size
Dynamic memory allocation allows you to allocate memory at runtime, which means you donβt need to define a fixed size for arrays or other structures. This is useful for handling variable data sizes.
In Python, what does the except keyword do in exception handling?
A Raises exceptions
B Defines exceptions
C Catches and ignores exceptions
D Catches exceptions
In Python, the except keyword is used to catch exceptions. It defines the block of code to be executed if an error occurs within the try block, allowing the program to continue running smoothly.
In Java, which of the following is the correct way to catch an exception?
A try {catch}
B throw (Exception e)
C catch Exception e {}
D try-catch()
In Java, exceptions are handled using try-catch blocks. The syntax is try { /* code */ } catch (Exception e) { /* exception handling code */ } to catch and handle any exceptions that may arise.
What is the result of accessing an array element out of bounds in C?
A Undefined behavior
B Syntax error
C Program crash
D Value 0
In C, accessing an array element outside of its bounds leads to undefined behavior. The program might crash, return garbage values, or even behave unpredictably, depending on the compiler and system configuration.
In C, what is the default value of a pointer before it is initialized?
A NULL
B Garbage value
C 0
D false
In C, uninitialized pointers contain a garbage value, meaning they hold unpredictable data. It is crucial to initialize pointers, often to NULL, before using them to avoid undefined behavior.
Which of these is a dynamic memory allocation function in C that initializes memory to zero?
A malloc()
B free()
C realloc()
D calloc()
In C, calloc() is used to allocate memory dynamically and initializes the memory block to zero. It is similar to malloc(), but calloc() also sets the allocated memory to zero, making it safer to use.