Final Exam (part 3) Flashcards

1
Q

public class Test {
int x;

 public Test(String t) {
      System.out.println("Test");
 }

 public static void main(String[] args) {
      Test test = new Test();
      System.out.println(test.x);
 } }
A

The program has a compile error because Test does not have a default constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What code may be filled in the blank without causing syntax or runtime errors?

public class Test {
java.util.Date date;

 public static void main(String[] args) {
      Test test = new Test();
      System.out.println(\_\_\_\_\_\_\_);
 } }

test.date
date
test.date.toString()
date.toString()

A

test.date

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

public class Test {
public static void main(String[] args) {
A a = new A ();
a.print();
}
}

class A {
String s;

 A (String s) {
      this.s = s;
 }

 void print() {
      System.out.println (s);
 } }

__The program has a compilation error because class A is not a public class.
__The program has a compilation error because class A does not have a default constructor.
__The program compiles and runs fine and prints nothing.
__The program would compile and run if you change A a = new A() to A a = new A(“5”).

A

The program has a compilation error because class A does not have a default constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is wrong in the following code?

class TempClass {
int i;
public void TempClass(int j) {
int i = j;
}
}

public class C {
public static void main(String[] args) {
TempClass temp = new TempClass(2);
}
}

A

The program has a compilation error because TempClass does not have a constructor with an int argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How many JFrame objects can you create and how many can you display?

A

unlimited

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which of the following statements are true about an immutable object?

__The contents of an immutable object cannot be modified
__All properties of an immutable object must be private.
__All properties of an immutable object must be of primitive types.
__An object type property in an immutable object must also be immutable.
__An immutable object contains no mutator methods.

A

contents cannot be modified
properties must be private
object type properties must also be immutable
contains no mutator methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

You should add the static keyword in the place of ? in Line _____ in the following code:

1 public class Test {
2 private int age;
3
4 public ? int square(int n) {
5 return n * n;
6 }
7
8 public ? int getAge() {
9 }
10 }

A

line 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

public class Test {
public static void main(String[] args) {
int n = 2;
xMethod(n);

      System.out.println("n is " + n);
 }

 void xMethod(int n) {
      n++;
 } }
A

The code has a compile error because xMethod is not declared static.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which of the following is the correct statement to return a string from an array a of characters?

toString(a)
new String(a)
convertToString(a)
String.toString(a)

A

new String(a)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The StringBuilder methods _____ not only change the contents of a string buffer, but also returns a reference to the string buffer.

delete
append
insert
reverse
replace

A

delete
append
insert
reverse
replace

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Assume StringBuilder strBuf is “ABCDEFG”, after invoking ________, strBuf contains “ABCRRRRDEFG”.

A

strBuf.insert(3, “RRRR”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

You cannot append a string to a string buffer if the resulting new string exceeds the capacity. T/F

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which of the following statements will convert a string s into a double value d?

A

d = Double.parseDouble(s);
d = (new Double(s)).doubleValue();
d = Double.valueOf(s).doubleValue();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Two strings with same contents are ALWAYS allocated to the same object. T/F

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

public class Test {
public static void main(String[] args) {
String s = new String(“Welcome to Java”);
Object o = s;
String d = (String)o;
}
}

__When assigning s to o in Object o = s, a new object is created.
__When casting o to s in String d = (String)o, a new object is created.
__When casting o to s in String d (String)o, the contents of o is changed.
__s, o, and d reference the same String object.

A

s, o, and d reference the same String object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The equals method is defined in the Object class. Which of the following is correct to override it in the String class?

public boolean equals(String other)
public boolean equals(Object other)
public static boolean equals(String other)
public static boolean equals(Object other)

A

public boolean equals(Object other)

17
Q

Invoking ______ returns the number of elements in an ArrayList x.

A

x.size()

18
Q

If a method is declared protected in the superclass, you may declare the method private in the subclass. T/F

A

false

19
Q

You can always successfully cast a superclass to a subclass. T/F

A

false

20
Q

Which of the follow are true?

__Override the methods equals and toString defined in the Object class whenever possible.
__Override the hashCode method whenever the equals method is overridden. By contract, two equal objects must have the same hash code.
__A public default no-arg constructor is assumed if no constructors are defined explicitly.
__You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.

A

All of them ^^^^

21
Q

Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be _____.

A

true

22
Q

Encapsulation means ______.

that data fields should be declared private
that a class can extend another class
that a variable of supertype can refer to a subtype object
that a class can contain another class

A

the data fields should be declared private

23
Q

The getValue() method is overridden in two ways. Which one is correct?

I:
public class Test {
public static void main(String[] args) {
A a = new A ();
System.out.println (a.getValue ());
}
}
class B {
public String getValue() {
return “Any object”;
}
}
class A extends B {
public Object getValue () {
return “A string”;
}
}

II:
public class Test {
public static void main(String[] args) {
A a = new A ();
System.out.println(a.getValue());
}
}
class B {
public Object getValue() {
return “Any object”;
}
}
class A extends B {
public String getValue() {
returns “A string”;
}
}

A

II

24
Q

What is the output of running class C?

class A {
public A () {
System.out.println(
“The default constructor of A is invoked”);
}
}

class B extends A {
public B () {
System.out.println(
“The default constructor of B is invoked”);
}
}

public class C {
public static void main(String[] args) {
B b = new B();
}
}

A

The default constructor of A is invoked
The default constructor of B is invoked

25
Q

What is displayed on the console when running the following program?

class Test {
public static void main(String[] args) {
try {
method();
System.out.println(“After the method call”);
}
catch (RuntimeException ex) {
System.out.println(“RuntimeException”);
}
catch (Exception ex) {
System.out.println(“Exception”);
}
}

 static void method() throws Exception {
      try {
           String s = "5.6";
           Integer.parseInt(s);
           
           int i = 0;
           int y = 2 / i;
           System.out.println("Welcome to Java");
      }
      catch (RuntimeException ex) {
           System.out.println("RuntimeException");
      }
      catch (Exception ex) {
           System.out.println("Exception");
      }
 } }
A

The program displays RuntimeException followed by After the method call.

26
Q

What are the reasons to create an instance of the File class?

__to determine whether the file exists
__to obtain the properties of the file such as whether the file can be read, written, or is hidden
__to rename the file
__to delete the file
__to read/write data from/to a file

A

__to determine whether the file exists
__to obtain the properties of the file such as whether the file can be read, written, or is hidden
__to rename the file
__to delete the file

27
Q

The following code causes Java to throw ______.

int number = Integer.MAX_VALUE + 1;

A

no exceptions

28
Q

An instance of ______ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.

A

RuntimeException

29
Q

Suppose you enter 34.3 57.8 789, then press the ENTER key.

Scanner input = new Scanner(System.in);
double v1 = input.nextDouble();
double v2 = input.nextDouble();
String line = input.nextLine();

What does line contain?

A

After the last statement is executed,
line contains characters ‘ ‘, ‘7’, ‘8’, ‘9’.

30
Q

A method should not claim Error or RuntimeException in the method declaration, but it may throw exceptions of these types. T/F

A

true

31
Q

Write a method public boolean isEmailAddress(String s) which returns true if s is an email-address. That is: s contains one and only one @, no space, no tab, no new line characters.

A

public boolean isEmailAddress(String s) {
int at = 0;
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c == ‘@’) at++;
if(c == ‘ ‘ || c == ‘\n’ || c == ‘\t’) return false;
}
if(at == 0 || at > 1) return false;
return true;
}

32
Q

Write the definition of a class Telephone. The class has no constructers and one static method getFullNumber. The method accepts a String argument and returns it, adding 785- to the beginning of the argument.

A

class Telephone {
public static String getFullNumber(String s) {
return “785-“ + s;
}
}

33
Q

Write a method f that accepts an ArrayList contains String objects. The methods should return a String containing the first character of each string in ArrayList, in order in which they appear. Thus, if the ArrayList contains the Strings: “Hello everyone how are you?”, the return value of the method would be String Hehay

A

String f(ArrayList<String> a) {
char[] name = new char[a.size()];
for(int i = 0; i < a.size(); i++) {
String temp = a.get(i);
name[i] = temp.charAt(0);
}
String n = new String(name);
return n;
}</String>

34
Q

Declare a String type instance variable named id for Employee class. This variable shall be access by all subclasses of Employee class.

A

protected String id;

35
Q

Suppose you have the following class:
public class Account{
private String id;
private double balance;
//constructors and other methods
}

What attribute do you need to add so that you can track how many account objects you have created? Add that attribute and write the constructor with two arguments

A

public class Account {
private String id;
private double balance;
private static int accountsCreated;

 public Account(String id, double balance) {
      this.id = id;
      this.balance = balance;
      accountsCreated++;
 } }
36
Q

Given two Strings String s1 = “11223351638791377189728193”; String s2 = “983763978669976862724569282405742578”; and String variable s3. Write a piece of code so that s3 refers to a String whose integer value will be the product of integer values of s1 and s2.

A

idfk

37
Q

Suppose that Student class has three private attributers: int id; doublegpa; and String name. Write the constructor that has no arguments. Initialize id to be 1000; gpa to be 0.0, and name to be “No name yet”.

A

public Student() {
id = 1000;
gpa = 0.0;
name = “No name yet”;
}

38
Q
A