IS5103 Lesson 4 quiz

1. What is the output of the following code?

public class Fish {

  1. public static void main(String[] args) {
  2. int numFish = 4;
  3. String fishType = “tuna”;
  4. String anotherFish = numFish + 1;
  5. System.out.println(anotherFish + ” ” + fishType);
  6. System.out.println(numFish + ” ” + 1);
  7. } }

The code does not compile.

2. What is the output of the following code?
var numbers = “012345678”.indent(1);
numbers = numbers.stripLeading();
System.out.println(numbers.substring(1, 3));
System.out.println(numbers.substring(7, 7));
System.out.print(numbers.substring(7));
Each correct answer represents a part of the solution. Choose all that apply.

A)
12
78
A blank line

3. What is included in the output of the following code?

var base = “ewe
sheep\t”;
int length = base.length();
int indent = base.indent(2).length();
int translate = base.translateEscapes().length();
var formatted = “%s %s %s”.formatted(length, indent, translate);
System.out.format(formatted);

Each correct answer represents a part of the solution. Choose all that apply.

A)
10
11
16

4. What is the result of the following code?

  1. String s1 = “””
  2. purr”””;
  3. String s2 = “”;
    4.
  4. s1.toUpperCase();
  5. s1.trim();
  6. s1.substring(1, 3);
  7. s1 += “two”;
    9.
  8. s2 += 2;
  9. s2 += ‘c’;
  10. s2 += false;
    13.
  11. if ( s2 == “2cfalse”) System.out.println(“==”);
  12. if ( s2.equals(“2cfalse”)) System.out.println(“equals”);
  13. System.out.println(s1.length());
    Each correct answer represents a part of the solution. Choose all that apply.

A)
7
Equals

5. What is the result of the following code?

  1. var sb = new StringBuilder();
  2. sb.append(“aaa”).insert(1, “bb”).insert(4, “ccc”);
  3. System.out.println(sb);

A) Abbaccca

6 . Which of the following return 5 when run independently?
var string = “12345”;
var builder = new StringBuilder(“12345”);
Each correct answer represents a complete solution. Choose all that
apply.

A)
builder.charAt(4)
builder.replace(2, 4, “6”).charAt(3)
string.replace(“123”, “1”).charAt(2)

7. 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) {
var roar1 = “roar”;
var roar2 = new StringBuilder(“roar”);
new Lion().roar(roar1, roar2);
System.out.println(roar1 + ” ” + roar2);
} }

roar roar!!!

8. Which of these statements are true?

var letters = new StringBuilder(“abcdefg”);
Each correct answer represents a complete solution. Choose all that apply.

letters.substring(1, 2) returns a single-character String.
letters.substring(6, 5) throws an exception.

9. Which of the following can fill in the blank to print avaJ?

  1. var puzzle = new StringBuilder(“Java”);
  2. puzzle.____________;
  3. System.out.println(puzzle);
    Each correct answer represents a complete solution. Choose all that apply.

A) reverse()
append(“vaJ$”).delete(0, 3).deleteCharAt(puzzle.length() – 1)

10. What is the output of the following code?

. var s = “Hello”;

  1. var t = new String(s);
  2. if (“Hello”.equals(s)) System.out.println(“one”);
  3. if (t == s) System.out.println(“two”);
  4. if (t.intern() == s) System.out.println(“three”);
  5. if (“Hello” == s) System.out.println(“four”);
  6. if (“Hello”.intern() == t) System.out.println(“five”);
    Each correct answer represents a complete solution. Choose all that apply.

A) 1
3
4

  1. Which of these array declarations are not legal?
    Each correct answer represents a complete solution. Choose all that apply.

A)

  1. String beans[] = new beans[6];
  2. int[][] types = new int[];
  3. int[][] java = new int[][];

12. Which of the following are true about arrays?
Each correct answer represents a complete solution. Choose all that
apply.

  1. The first element is index 0.
  2. Arrays are fixed size.
  3. Calling equals() on two different arrays containing the
    same primitive values always returns false.

13. What is the output of the following?

var arr = new String[] { “PIG”, “pig”, “123”};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.binarySearch(arr, “Pippa”));
Each correct answer represents a complete solution. Choose all that
apply.

A) [123, PIG, pig]
-3

14. Which of the following fill in the blank to print a positive integer?

String[] s1 = { “Camel”, “Peacock”, “Llama”};
String[] s2 = { “Camel”, “Llama”, “Peacock”};
String[] s3 = { “Camel”};
String[] s4 = { “Camel”, null};
System.out.println(Arrays.__________);
Each correct answer represents a complete solution. Choose all that apply.

A) 1. compare(s1, s2)
mismatch(s1, s2)
mismatch(s3, s4)

15. How many of these lines contain a compiler error?

  1. double one = Math.pow(1, 2);
  2. int two = Math.round(1.0);
  3. float three = Math.random();
  4. var doubles = new double[] {one, two, three};

2

16. How many of these lines contain a compiler error?

  1. int one = Math.min(5, 3);
  2. long two = Math.round(5.5);
  3. double three = Math.floor(6.6);
  4. var doubles = new double[] {one, two, three};

0

17. Note that March 13, 2022 is the weekend when we spring
forward, and November 6, 2022 is when we fall back for daylight saving
time. Which of the following can fill in the blank without the code
throwing an exception?

var zone = ZoneId.of(“US/Eastern”);
var date = ___________________;
var time = LocalTime.of(2, 15);
var z = ZonedDateTime.of(date, time, zone);
Each correct answer represents a complete solution. Choose all that apply.

A) 1. LocalDate.of(2022, 3, 13)
LocalDate.of(2022, 11, 6)
LocalDate.of(2022, 11, 7)

18. Which of these statements are true for the two values?
2022–08–28T05:00 GMT-04:00
2022–08–28T09:00 GMT-06:00

Each correct answer represents a complete solution. Choose all that apply.

A) 1. The first date/time is earlier.
The date/times are six hours apart.

19. What is the output of the following code?

var date = LocalDate.of(2022, 4, 3);
date.plusDays(2);
date.plusHours(3);
System.out.println(date.getYear() + ” ” + date.getMonth()
” ” + date.getDayOfMonth());

The code does not compile.

20. Given the following, which can correctly fill in the blank?
var date = LocalDate.now();
var time = LocalTime.now();
var dateTime = LocalDateTime.now();
var zoneId = ZoneId.systemDefault();
var zonedDateTime = ZonedDateTime.of(dateTime, zoneId);
Instant instant = __________________;
Each correct answer represents a complete solution. Choose all that apply.

A) Instant.now()
zonedDateTime.toInstant()

  1. Note that March 13, 2022 is the weekend that clocks spring ahead for
    daylight saving time. What is the output of the following?
    var date = LocalDate.of(2022, Month.MARCH, 13);
    var time = LocalTime.of(1, 30);
    var dateTime1 = ZonedDateTime.of(date, time, zone);
    var dateTime2 = dateTime1.plus(1, ChronoUnit.HOURS);
    long diff = ChronoUnit.HOURS.between(dateTime1, dateTime2);
    int hour = dateTime2.getHour();
    boolean offset = dateTime1.getOffset()
    == dateTime2.getOffset();
    System.out.println(“diff = ” + diff);
    System.out.println(“hour = ” + hour);
    System.out.println(“offset = ” + offset);
    Each correct answer represents a part of the solution. Choose all that apply

A) diff = 1
hour = 3

22. What is the output of the following code?
var date = LocalDate.of(2022, Month.APRIL, 30);
date.plusDays(2);
date.plusYears(3);
System.out.println(date.getYear() + ” ” + date.getMonth()
” ” + date.getDayOfMonth());

A) 2022 APRIL 30

Other Links:

Statistics Quiz

Networking Quiz

See other websites for quiz:

Check on QUIZLET

Check on CHEGG

Leave a Reply

Your email address will not be published. Required fields are marked *