Exercises
Exercises
Instructions:
- You are allowed to use any resources (offline or online) to complete
these exercises. However, the assessment is meant to be completed
individually and should not be discussed with anyone else.
A. 35
35
B. 35
35.0
C. 35.0
35
D. No Output.
A. true
B. false
C. null
D. 0
A. public
B. final
C. restricted
D. static
4. What is it called when a class can have more than one method with the same
name, if their arguments are different?
A. Overriding
B. Overloading
C. Polymorphism
D. Inheritance
A. True
B. False
A. Class
B. Constant
C. Integer
D. Array
A. int
B. char
C. double
D. boolean
8. What is the keyword for creating a method that can be called without creating
any object instances?
A. final
B. static
C. public
D. hidden
10. Which of the following type conversions are not automatic in Java?
int[] a = {1,2,3};
int sum = 0;
for(int i = 0; i < a.length; i++)
{
sum+=a[i];
System.out.println(sum);
}
}
17. Which of the following keywords is used for creating objects in Java?
A. create
B. instanceOf
C. new
D. return
18. If a variable is declared private, this restricts access of the variable to:
class Base
{
public void show()
{
System.out.println("Superclass’ show() called");
}
}
23. Which of the following corresponds to the last index of an ArrayList called
nums?
A. nums.size()-1
B. nums.length()-1
C. nums.size()
D. nums.length()
A. int
B. Integer
C. double
D. None of the above
25. What interface should a class implement in order to react to button clicks?
A. TextListener
B. Runnable
C. ActionListener
D. ImageConsumer
26. Complete the following code to make TestGUI visible:
import java.awt.*;
import javax.swing.*;
A. Photoshop Toolkit
B. ActionListener Toolkit
C. Abstract Window Toolkit
D. Java Enhancement Toolkit
A. BorderLayout
B. GridLayout
C. FlowLayout
D. CardLayout
A. The same program logic can be used with objects of several related types.
B. It reduces the number of compile-time errors in a program.
C. The program’s execution time decreases.
D. None of the above.
31. What is the output of the following program?
A. c = 241
B. c = 28
C. c = 0
D. Compilation Error (No output)
A. 21
33
B. 31
23
C. 21.0
33.0
D. 31.0
23.0
34. From the words listed in the “Word Pool” below, pick and choose the word
that best matches each question. Write the correct word in the answer box next to
each question. Each word matches exactly one question.
Word Pool
static
public
final
setVisible()
FlowLayout
class
break
object
Integer.parseInt()
Question Answer
a) What is the exception thrown when we try to compile the code? Explain.
b) How can you fix the for loops in line1 and line2?
c) Once the code is fixed, what is the output?