C/C++ Java Quiz Answers
1. What is the main purpose of the inline keyword in C/C++?
*
1 point
To speed up function execution by avoiding a function call
To enforce memory alignment
To define a function only inside a header
To allocate memory on the stack
2. What is the default storage class of a variable declared inside a function?
*
1 point
auto
static
extern
register
Option 2
3. Which of the following best describes the virtual keyword in C++?
*
1 point
It ensures the function is overridden
It allows dynamic dispatch at runtime
It prevents a function from being overridden
It makes the class abstract
4. Which keyword in C++ is used to prevent a class from being inherited?
1 point
const
delete
final
protected
5. Which of the following statements is true about function overloading in C++?
*
1 point
Overloading must differ only in return type
Overloading must differ in parameter list
You can overload based on default arguments
You cannot overload a member function
6. What will be the output of the following code?
int main() {
int a = 5;
printf("%d", a++);
}
*
1 point
5
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/8
6
0
Undefined behaviour
7. What is the output of this code?
int main() {
char str[] = "HELLO";
str[0] = 'h';
printf("%s", str);
}
*
1 point
HELLO
hELLO
Compilation error
Segmentation fault
8. What will this output?
int main() {
int x = 10;
if (x++ == 10 && ++x == 12)
printf("Yes");
else
printf("No");
}
*
1 point
Yes
No
12
Undefined Behaviour
9. What will this print?
int f() {
static int count = 3;
if (--count)
return f() * count;
else
return 1;
}
int main() {
printf("%d", f());
}
*
1 point
6
1
2
0
10. Which of the following correctly creates a pointer to an array of 10 integers in C?
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/8
*
1 point
int *ptr[10];
int (*ptr)[10];
int *(ptr)[10];
int ptr*(10);
11. What is the main purpose of the final keyword for a variable in Java?
*
1 point
To mark a variable for garbage collection
To declare a constant that cannot be reassigned
To make a variable global
To allocate variable in the heap
12. What is the default value of an instance variable of type boolean in Java?
*
1 point
True
False
Null
Undefined
13. Which of the following best describes method overloading in Java?
*
1 point
Two methods with the same name and parameters but different return types
Two methods with the same name but different parameter lists
Two methods in different classes with the same name
Overriding methods from the parent class
14. Which keyword in Java is used to prevent a class from being extended?
*
1 point
static
final
abstract
sealed
15. Which of the following statements about Java interfaces is true?
*
1 point
An interface can have private constructors
An interface can extend multiple interfaces
An interface can be instantiated directly
An interface cannot have default methods
16. What will be the output of the following code?
public class Test {
public static void main(String[] args) {
int a = 5;
System.out.println(a++);
}
}
*
1 point
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/8
5
6
0
Compilation error
17. What will be the output of this code?
public class Test {
public static void main(String[] args) {
String str = "HELLO";
str = "h" + str.substring(1);
System.out.println(str);
}
}
*
1 point
HELLO
hELLO
Compilation error
NullPointerException
18. What will this output?
public class Test {
public static void main(String[] args) {
int x = 10;
if (x++ == 10 && ++x == 12)
System.out.println("Yes");
else
System.out.println("No");
}
}
*
1 point
Yes
No
12
Compilation error
19. What will this method return?
public class Test {
static int count = 3;
public static int f() {
if (--count != 0)
return f() * count;
else
return 1;
}
public static void main(String[] args) {
System.out.println(f());
}
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/8
*
1 point
6
1
2
0
20. Which of the following correctly creates an array of 10 integers in Java?
*
1 point
int ptr[10];
int[] ptr = new int[10];
int *ptr = new int[10];
int ptr = int[10];
21. What is the output of print(type([]))?
*
1 point
<class 'list'>
<type 'list'>
list
['list']
22. Which of the following is not a valid Python data type?
*
1 point
dictionary
tuple
array
set
23. What is the default return value of a function that doesn’t explicitly return anything?
*
1 point
0
undefined
None
void
24. What is the output of the following code?
x = [1, 2, 3]
y=x
y.append(4)
print(x)
*
1 point
[1, 2, 3]
[1, 2, 3, 4]
[4]
Error
25. Which of the following statements is true about Python variables?
*
1 point
They must be declared with type
They point to memory like C pointers
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/8
They are names bound to objects
They are allocated at compile-time
26. What will be the output of this code?
def func(a, L=[]):
L.append(a)
return L
print(func(1))
print(func(2))
*
1 point
[1] then [2]
[1] then [1, 2]
[1] then [1]
[2] then [1, 2]
27. Which keyword is used to define an anonymous function in Python?
*
1 point
def
func
lambda
return
28. What will this code print?
x = 10
def modify():
global x
x=x+5
modify()
print(x)
*
1 point
10
5
15
Error
29. Which of these is the correct way to catch exceptions in Python?
*
1 point
catch(Exception e):
except Exception as e:
try {}
throw Exception:
30. What is the output of this code?
def f():
try:
return 1
finally:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/8
return 2
print(f())
*
1 point
1
2
None
Error answer
Here are the correct answers to the 30 multiple-choice questions:
1. ✅ To speed up function execution by avoiding a function call
2. ✅ auto
3. ✅ It allows dynamic dispatch at runtime
4. ✅ final
5. ✅ Overloading must differ in parameter list
6. ✅ 5
7. ✅ hELLO
8. ✅ Yes
9. ✅ 2
10. ✅ *int (ptr)[10];
11. ✅ To declare a constant that cannot be reassigned
12. ✅ False
13. ✅ Two methods with the same name but different parameter lists
14. ✅ final
15. ✅ An interface can extend multiple interfaces
16. ✅ 5
17. ✅ hELLO
18. ✅ Yes
19. ✅ 2
20. ✅ int[] ptr = new int[10];
21. ✅ <class 'list'>
22. ✅ array
23. ✅ None
24. ✅ [1, 2, 3, 4]
25. ✅ They are names bound to objects
26. ✅ [1] then [1, 2]
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/8
27. ✅ lambda
28. ✅ 15
29. ✅ except Exception as e:
30. ✅ 2
Let me know if you’d like explanations for any of the answers!
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/8