Chapter 3 : Core Java APIs Flashcards Preview

Java 8 > Chapter 3 : Core Java APIs > Flashcards

Flashcards in Chapter 3 : Core Java APIs Deck (54)
Loading flashcards...
1

What will be the output of the following source?

String name1 = "Fluffy";
String name2 = new String("Fluffy");
String name3 = "Fluffy";
boolean x = name1 == name2;
boolean y = name1 == name3;
System.out.println(x + " " + y);

a) true true
b) true false
c) false false
d) false true

d) false true

boolean x = name1 == name2; is false because the new String() creates a new object

boolean y = name1 == name3; is true because the literal "Fluffy" has one single immutable object in the String pool and therefore both references x and y point to it.

2

What will be the output of the following source?

int three = 3;
String four = "4";
System.out.println(1 + 2 + three + four);

a) 1234
b) 64
c) 334
d) 10

b) 64

When doing String concatenation using the + operator, the statement is processed in pairs left-to-right

3

What will be the output of the following source?

String s = "1";
s += "2";
s += 3;
System.out.println(s);

a) 15
b) 123
c) 1223
d) 11223

b) 123

4

What method will give the expected result? Choose all that apply.

String string = "animals";
System.out.println(string.____()); // Expected 7

a) size
b) charAt
c) indexOf
d) length

d) length

5

What method will give the expected result? Choose all that apply.

String string = "animals";
System.out.println(string.____); // Expected s

a) indexOf(6)
b) indexOf(7)
c) charAt(6)
d) charAt(7)

c) charAt(6)

a) and b) are wrong because indexOf() returns an int

d) will throw an exception since it is out of bounds

6

What method will give the expected result? Choose all that apply.

String string = "animals";
System.out.println(string.____); // Expected 4

a) indexOf("al")
b) indexOf("al", 4)
c) contains("al")
d) contains("al", 4)

a) indexOf("al") & b) indexOf("al", 4)

b) indexOf("al", 4) searches for the string "al" starting at position 4. Since "al" occurs starting at 4, it returns immediately.

c) and d) are wrong because contains() returns a boolean

7

What method will give the expected result? Choose all that apply.

String string = "animals";
System.out.println(string.____); // Expected mals

a) substring(3)
b) substring(string.indexOf('m')));
c) substring(3,6);
d) substring(3,7);

a), b), d)

c) substring(3,6); would result in "mal"

8

What will be the output of the following source?

String string = "animals";
System.out.println(string.substring(3,2));

a) i
b) mals
c) Does not compile
d) Exception thrown

d) Exception thrown

9

What method will give the expected result? Choose all that apply.

String string = "animals";
System.out.println(string.____("animals")); // Expected true

a) equals
b) compare
c) equalsIgnoreCase
d) compareTo

a) equals & c) equalsIgnoreCase

b) compare, is not a valid method

d) compareTo is a method that does lexicographical comparison and it returns an integer. 0 means the strings are equals. 

10

What will be the output of the following source?

String string = "animals";
System.out.println(string.equals(null));

a) false
b) true
c) Exception is thrown
c) Does not compile

a) false

equals does not throw an exception when parameter is null

11

What will be the output of the following source? String string = "animals"; System.out.println(string.compareTo(null)); a) false b) -1 c) Exception is thrown c) Does not compile

c) Exception is thrown compareTo throws an exception when parameter is null

12

What method will give the expected result? Choose all that apply. String string = "animals"; System.out.println(string.____)); // Expected AnimAls a) toUpperCase("a") b) toUpperCase('a') c) replace("a", "A") d) replace('a', 'A')

c) and d) a) and b) are wrong since those methods act on the whole string and do not take a String or char parameter

13

What will be the output of the following source? (Note that \t is a invisible tab) String string = "\tA\tBC\t"; System.out.print(string.trim()); System.out.println("C"); a) \tA\tBC b) A\tBC c) ABC c) Does not compile

b) A\tBC

14

What will be the output of the following source? 4 StringBuilder a = new StringBuilder("abc"); 5 StringBuilder b = a.append("de"); 6 b = b.append("f").append("g"); 7 System.out.println("a=" + a); 8 System.out.println("b=" + b); a) a=abc , b=abcdefg b) a=abc , b=fg c) a=abc , b=g d) a=abcdefg, b=abcdefg

d) a=abcdefg, b=abcdefg Both a and b are pointing to the same object. append() returns a reference to the object itself.

15

How many strings are created? StringBuilder alpha = ""; for (char current='a'; current <= 'z'; current++) { alpha.append(current); } System.out.println(alpha); a) 1 b) 27 c) Does not compile d) Exception thrown

c) StringBuilder cannot be assigned a String literal. The following would work: StringBuilder alpha = new StringBuilder();

16

What will be the output of the following source? StringBuilder alpha = new StringBuilder(10); alpha.append(12345); System.out.println(alpha.length()); a) 5 b) 10 c) Does not compile d) Exception thrown

a) The constructor takes in 10 which means the object can hold up to 10 characters. However capacity != length

17

What will be the output of the following source? StringBuilder alpha = new StringBuilder().append(1).append('c'); alpha.append("-").append(true); System.out.println(alpha); a) 1c b) 1c-true c) Does not compile d) Exception thrown

b) 1c-true

18

What is the difference between StringBuilder and StringBuffer?

They do the same thing except StringBuffer is thread safe.

19

What will be the output of the following source? public class Junk { String name; public static void main(String[] args) { Junk a = new Junk(); Junk b = new Junk(); Junk c = a; System.out.print((a == c) + " "); System.out.print((a == b) + " "); System.out.print(a.equals(b)); } } a) false false false b) true false true c) true false false d) true true true

c) true false false a.equals(b) is false because, when equals() is not implemented, then the default behavior is to compare references.

20

What statements will compile? Choose all that apply. 1 int[] a; 2 int [] b; 3 int c[]; 4 int d []; 5 int[] e, f; 6 int g[], h; 7 int i[ ]; a) 1, 3 b) 1, 3, 5, 6 c) 1, 2, 5 d) 1, 2, 5, 7 e) All of them compile

e) All of them compile

21

How many arrays are declared? Choose all that apply. int[] a, b; int c[], d; a) 1 b) 2 c) 3 d) 4

c) 3 a, b, c are arrays d is just an int

22

Which of the following is true? Select all that apply. 3 String[] strings = { "apples" }; 4 Object[] objects = strings; 5 String[] moreStrings = (String[]) objects; 6 moreStrings[0] = new StringBuilder(); 7 objects[0] = new StringBuilder(); a) 5 does not compile b) 6 does not compile c) 6 throws exception d) 7 does not compile e) 7 throws exception

b) 6 does not compile &amp; e) 7 throws an exception 7 throws a ArrayStoreException

23

What options will give the expected result? Choose all that apply. int[] numbers = new int[10]; for (int i = 0; i == 4; i++) { numbers[i] = i; } System.out.println(numbers.____); // Expected 5 a) size() b) length() c) length d) None of the above

d) None of the above The array is initialized to be of size 10, therefore lenght is 10 regardless of how many values are in the array. numbers.length will print 5

24

What is the output of the following? int[] numbers = new int[10]; for (int i = 0; i == numbers.length; i++) { numbers[i] = i; } System.out.println(numbers); a) [I@4d7e1886 b) 0123456789 c) Does not compile d) None of the above

a) [I@4d7e1886 The above [ means it is an array. The letter "I" means its an int. The string after the "@" is a hash value for the memory address

25

What is the output of the following? String[] strings = { "10", "9", "100" }; Arrays.sort(strings); for (String string : strings) System.out.print(string + " "); a) 10 100 9 b) 9 10 100 c) Does not compile d) Exception thrown

a) 10 100 9 Strings are sorted in alphabetic order. "100" comes before "9"

26

What is the output of the following? int[] numbers = {2, 4, 6, 8}; System.out.print(Arrays.binarySearch(numbers, 2) + " "); System.out.print(Arrays.binarySearch(numbers, 4) + " "); System.out.print(Arrays.binarySearch(numbers, 1) + " "); System.out.print(Arrays.binarySearch(numbers, 3) + " "); System.out.print(Arrays.binarySearch(numbers, 9) + " "); a) 0 1 0 1 4 b) 0 1 -1 -2 4 c) 0 1 0 1 5 d) 0 1 -1 -2 -5 e) None of the above

d) 0 1 -1 -2 -5 If binarySearch cannot find a value then it returns the index at which the missing value should be inserted in such a way that it will maintain the ascending sorted order. Then, it negates that values and substracts 1. For example, 9 : -[4] - 1 = -5

27

What is the output of the following? int[] numbers = {4, 2, 6, 8}; System.out.print(Arrays.binarySearch(numbers, 2) + " "); System.out.print(Arrays.binarySearch(numbers, 4) + " "); System.out.print(Arrays.binarySearch(numbers, 1) + " "); System.out.print(Arrays.binarySearch(numbers, 3) + " "); System.out.print(Arrays.binarySearch(numbers, 9) + " "); a) 0 1 0 1 4 b) 0 1 -1 -2 4 c) 0 1 0 1 5 d) 0 1 -1 -2 -5 e) None of the above

e) None of the above The array is not sorted and therefore binarySearch is unpredictable.

28

What is the output of the following? public static void main(String...args) { System.out.print(args[0] + " "); System.out.println(args.length); } a) Prints the first argument plus the length of args b) Prints the first argument c) Does not compile d) Exception thrown

a) Prints the first argument plus the length of args Varargs are treated like arrays

29

What statements will compile? Choose all that apply. 1 int[][] vars1; 2 int[] vars2[]; 3 int[] vars3[][]; 4 int[][] vars4 = {{1,4},{3},{3,6,9}}; a) 1, 4 b) 1, 3 c) 1, 2, 4 d) All statements compile

d) All statements compile

30

What method will give the expected result? Choose all that apply. ArrayList list = new ArrayList(); list.add("hawk"); // ["hawk"] list.add("owl"); // ["hawk","owl"] list.________; // ["pigeon", "owl"] a) add(0, "pigeon") b) add(1, "pigeon") c) set(0, "pigeon") d) set(1, "pigeon")

c) set(0, "pigeon")