You are on page 1of 14

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.

1. Given the following code snippet:

public static void main(String[] args)


{
int a = 5;
int b = 7;
int c = a*b;
double d = a*b;
System.out.println(c);
System.out.println(d);
}

What will be printed?

A. 35
35

B. 35
35.0

C. 35.0
35

D. No Output.

2. What is the default value of an instance variable of type boolean?

A. true
B. false
C. null
D. 0

3. Which of the following is a Java access modifier?

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

5. The length() of the String “Hello World” is 11

A. True
B. False

6. In this code, args is considered to be a/an,

public static void main(String[] args)


{
//some code;
}

A. Class
B. Constant
C. Integer
D. Array

7. What is the return type for the String function length()?

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

9. What is the output of the following code:

public static void main(String[] args)


{
double a = 10.5 % 2;
double b = 10.5 / 2;
double c = 10.5 * 2;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}

10. Which of the following type conversions are not automatic in Java?

A. int => double


B. char => int
C. double => short
D. float => double

11. What is the output of each System.out.println in the following program?

public class LabTest


{
public static void main(String[] args)
{
String str = "CNIT 255";
System.out.println(str.replaceAll("5","0"));
System.out.println(str.concat("01").concat("01"));
System.out.println(str.contains("it"));
System.out.println(str.length());
}
}

12. What does the following code print?

public static void main(String[] args)


{
int x = 0;
while(x < 10)
{
x = 3*x + 1;
}
System.out.println(x);
}

13. What is the output of the following main function?

public static void main(String[] args)


{
String[] vowels = {"a", "e", "i", "o", "u",};
System.out.println(vowels[1] + vowels[3]);
System.out.println(vowels[4]);
}

14. What is the output of the following code?

public static void main(String[] args)


{

int[] a = {1,2,3};
int sum = 0;
for(int i = 0; i < a.length; i++)
{
sum+=a[i];
System.out.println(sum);
}
}

15. What will happen when the following code is executed?

public static void main(String[] args)


{
String[] str = {"a","b","c","d","e"};
System.out.println(str[5]);
}

16. What is the output for the following code?

Inside the file Student.java:


public class Student
{
String name;
public Student(String s)
{
name = s;
}
public Student()
{
name = "Unknown";
}
}

Inside the file Course.java:


public class Course
{
public static void main(String[] args)
{
Student s = new Student("xyz");
Student a = new Student();
System.out.println(s.name);
System.out.println(a.name);
}

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:

A. The package the variable is defined in.


B. The class the variable is defined in.
C. The method the variable is defined in.
D. The program the variable is defined in.

19. What is the output of the following main program?

public static void main(String[] args)


{
String test = "abcdef";
String str = "JAVA OOP";
System.out.println(test.substring(5));
System.out.println(test.substring(0,3));
System.out.println(str.substring(7));
System.out.println(str.substring(0,5));
}
20. What is the output of the following code?
(Hint: There is a bug in the constructor.)

public class Student


{
private String name = "MyName";
public Student(String value)
{
String name = value;
}
public String getName()
{
return name;
}
public static void main(String[] args)
{
Student student = new Student("Bill");
System.out.println(student.getName());
}
}

21. What is the output of the following program?

class Base
{
public void show()
{
System.out.println("Superclass’ show() called");
}
}

class Derived extends Base


{
public void show()
{
System.out.println("Subclass’ show() called");
}
}

public class Main


{
public static void main(String[] args)
{
Base b = new Derived();
b.show();
}
}

22. If class X extends class Y, which of the following is true?

A. X is the superclass, Y is the subclass


B. Y is the superclass, X is the subclass

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

24. Which of the following is a wrapper class in Java?

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.*;

public class TestGUI extends JFrame


{
public TestGUI()
{
JButton yellow = new JButton("yellow");
JButton blue = new JButton("blue");
JPanel buttonPanel = new JPanel();
buttonPanel.add(yellow);
buttonPanel.add(blue);
add(buttonPanel);
}
}

public class TestFrame


{
public static void main (String[] args)
{
JFrame frm = new ();
frm. (150, 100);
frm. (true);
}
}

27. One of the original GUI toolkits in Java was called:

A. Photoshop Toolkit
B. ActionListener Toolkit
C. Abstract Window Toolkit
D. Java Enhancement Toolkit

28. What is the default Layout Manager for JFrame?

A. BorderLayout
B. GridLayout
C. FlowLayout
D. CardLayout

29. Given the following code:

Inside the file Bank.java:


public abstract class Bank
{
abstract void withdraw();
abstract void deposit();

public void balance()


{
// implementation for balance here
}
}

Inside the file Customer.java:


public class Customer extends Bank
{
void deposit()
{
// implementation for deposit here
}
}

Why does this code not compile?

A. The abstract keyword cannot be used with classes, only methods


B. The Bank class cannot have a concrete method like balance
C. The Customer class cannot extend an abstract class
D. The Customer class implements only 1 of the abstract methods of Bank, when it
needs to implement both

30. What is the primary advantage of polymorphism?

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?

public class Main


{
public static void main(String[] args)
{
int a = 9;
int b = 37;
int c = (a * 7) + (2 - b);
System.out.println("c = " + c);
}
}

A. c = 241
B. c = 28
C. c = 0
D. Compilation Error (No output)

32. Please read the following class declarations:

public class Employee


{
// some code
}

public class DepartmentHead extends Employee


{
// some code
}

Please circle all of the following which are true:

1. The Employee class is a superclass of the DepartmentHead class


2. The DepartmentHead class cannot add its own methods that don’t exist in the
Employee class
3. The DepartmentHead class can override public non-final methods in the
Employee class

33. Given the following code:


public class Main
{
public static double sum(int a, int b)
{
return (a+b);
}

public static double sum(double x, double y, double z)


{
return (x+y+z);
}

public static void main(String[] args)


{
int num1 = 10;
int num2 = 11;
int num3 = 12;
double a = sum(num1, num2);
double b = sum(num1,num2,num3);
System.out.println(a);
System.out.println(b);
}
}

What is the 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

This keyword is used to define a variable that can


be assigned only once.

This is used to convert a string to an int.

I am a blueprint (or template) from which objects


can be created.

A keyword used to terminate a case in the switch


statement.

This keyword can make a method accessible


without having to create objects of the class in
which it is defined.

This is the instance of a class.

The default layout of a JPanel.

Avoid declaring instance variables with this


modifier whenever possible.

Used to make a JFrame visible.


35. Given the following code, answer all three questions below.
(Hint: There are bugs in the lines highlighted with red.)

public static void main(String[] args)


{
String startString = "Text to be printed.";
String wordArray[] = startString.split(" ");
String printArray[] = new String[wordArray.length];
for(int i = 0; i <= wordArray.length; i++) //line1
{
printArray[(printArray.length - 1) - i] = wordArray[i];
}
for(int j = 0; j <= printArray.length; j++) //line2
{
if(j > 0)
{
System.out.print(" ");
}
printArray[j] = printArray[j].replace(".", "");
System.out.print(printArray[j]);
}
System.out.println(".");
}

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?

You might also like