Ask Java Expert


Home >> Java

Multiple Choice Assignment.

Reading:

1- Think Java: Chapter 8, 11

2.OCA: Chapter 3, 4

Exercise on paper: (Total 8 points)

Ch. 3

1. What is output by the following code? (Choose all that apply)

1: public class Fish {

2:  public static void main(String[] args) {

3:    intnumFish = 4;

4:    String fishType = "tuna";

5:    String anotherFish = numFish + 1;

6:    System.out.println(anotherFish + " " + fishType);

7:    System.out.println(numFish + " " + 1);

8:  } }

A.   4 1

B.    41

C.    5

D.   5 tuna

E.    5tuna

F.     51tuna

G.   The code does not compile.

2.     Which of the following are output by this code? (Choose all that apply)

3: String s = "Hello";

4: String t = new String(s);

5: if ("Hello".equals(s)) System.out.println("one");

6: if (t == s) System.out.println("two");

7: if (t.equals(s)) System.out.println("three");

8: if ("Hello" == s) System.out.println("four");

9: if ("Hello" == t) System.out.println("five");

A.   one

B.    two

C.    three

D.   four

E.    five

F.     The code does not compile.

3.     Which are true statements? (Choose all that apply)

A.   An immutable object can be modified.

B.    An immutable object cannot be modified.

C.    An immutable object can be garbage collected.

D.   An immutable object cannot be garbage collected.

E.    String is immutable.

F.     StringBuffer is immutable.

G.   StringBuilder is immutable.

4.     What is the result of the following code?

7: StringBuildersb = new StringBuilder();

8: sb.append("aaa").insert(1, "bb").insert(4, "ccc");

9: System.out.println(sb);

A.   abbaaccc

B.    abbaccca

C.    bbaaaccc

D.   bbaaccca

E.    An exception is thrown.

F.     The code does not compile.

5.     What is the result of the following code?

2: String s1 = "java";

3: StringBuilder s2 = new StringBuilder("java");

4: if (s1 == s2)

5:  System.out.print("1");

6: if (s1.equals(s2))

7:  System.out.print("2");

A.   1

B.    2

C.    12

D.   No output is printed.

E.    An exception is thrown.

F.     The code does not compile.

6.     What is the result of the following code?

public class Lion {

public void roar(String roar1, StringBuilder roar2) {

    roar1.concat("!!!");

    roar2.append("!!!");

  }

public static void main(String[] args) {

  String roar1 = "roar";

StringBuilder roar2 = new StringBuilder("roar");

new Lion().roar(roar1, roar2);

System.out.println(roar1 + " " + roar2);

} }

A.   roar roar

B.    roar roar!!!

C.    roar!!! roar

D.   roar!!! roar!!!

E.    An exception is thrown.

F.     The code does not compile.

7.     Which are the results of the following code? (Choose all that apply)

String letters = "abcdef";

System.out.println(letters.length());

System.out.println(letters.charAt(3));

System.out.println(letters.charAt(6));

A.   5

B.    6

C.    c

D.   d

E.    An exception is thrown.

F.     The code does not compile.

8.     Which are the results of the following code? (Choose all that apply)

String numbers = "012345678";

System.out.println(numbers.substring(1, 3));

System.out.println(numbers.substring(7, 7));

System.out.println(numbers.substring(7));

A.   12

B.    123

C.    7

D.   78

E.    A blank line.

F.     An exception is thrown.

G.   The code does not compile.

9.     What is the result of the following code?

3: String s = "purr";

4: s.toUpperCase();

5: s.trim();

6: s.substring(1, 3);

7: s += " two";

8: System.out.println(s.length());

A.   2

B.    4

C.    8

D.   10

E.    An exception is thrown.

F.     The code does not compile.

10. What is the result of the following code? (Choose all that apply)

13: String a = "";

14: a += 2;

15: a += 'c';

16: a += false;

17: if ( a == "2cfalse") System.out.println("==");

18: if ( a.equals("2cfalse")) System.out.println("equals");

A.   Compile error on line 14.

B.    Compile error on line 15.

C.    Compile error on line 16.

D.   Compile error on another line.

E.    ==

F.     equals

G.   An exception is thrown.

11. What is the result of the following code?

4: int total = 0;

5: StringBuilder letters = new StringBuilder("abcdefg");

6: total += letters.substring(1, 2).length();

7: total += letters.substring(6, 6).length();

8: total += letters.substring(6, 5).length();

9: System.out.println(total);

A.   1

B.    2

C.    3

D.   7

E.    An exception is thrown.

F.     The code does not compile.

12. What is the result of the following code? (Choose all that apply)

StringBuilder numbers = new StringBuilder("0123456789");

numbers.delete(2,  8);

numbers.append("-").insert(2, "+");

System.out.println(numbers);

A.   01+89-

B.    012+9-

C.    012+-9

D.   0123456789

E.    An exception is thrown.

F.     The code does not compile.

13. What is the result of the following code?

StringBuilder b = "rumble";

b.append(4).deleteCharAt(3).delete(3, b.length() - 1);

System.out.println(b);

A.   rum

B.    rum4

C.    rumb4

D.   rumble4

E.    An exception is thrown.

F.     The code does not compile.

14. Which of these compile when replacing line 8? (Choose all that apply)

7: char[]c = new char[2];

8: // INSERT CODE HERE

A.   int length = c.capacity;

B.    int length = c.capacity();

C.    int length = c.length;

D.   int length = c.length();

E.    int length = c.size;

F.     int length = c.size();

G.   None of the above.

15. Which of these compile when replacing line 8? (Choose all that apply)

7: ArrayList l = new ArrayList();

8: // INSERT CODE HERE

A.   int length = l.capacity;

B.    int length = l.capacity();

C.    int length = l.length;

D.   int length = l.length();

E.    int length = l.size;

F.     int length = l.size();

G.   None of the above.

Ch. 4

1.     Which of the following can fill in the blank in this code to make it compile? (Choose all that apply)

public class Ant {

  _____ void method() { }

}

A.   default

B.    final

C.    private

D.   Public

E.    String

F.     zzz:

2.     Which of the following compile? (Choose all that apply)

A.   final static void method4() { }

B.    public final int void method() { }

C.    private void int method() { }

D.   static final void method3() { }

E.    void final method() {}

F.     void public method() { }

3.     Which of the following methods compile? (Choose all that apply)

A.   public void methodA() { return;}

B.    public void methodB() { return null;}

C.    public void methodD() {}

D.   public intmethodD() { return 9;}

E.    public intmethodE() { return 9.0;}

F.     public intmethodF() { return;}

G.   public intmethodG() { return null;}

Java, Programming

  • Category:- Java
  • Reference No.:- M92256004
  • Price:- $40

Priced at Now at $40, Verified Solution

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As