Working with Strings, Arrays, and ArrayLists Flashcards Preview

JAVA OCA7 > Working with Strings, Arrays, and ArrayLists > Flashcards

Flashcards in Working with Strings, Arrays, and ArrayLists Deck (42)
Loading flashcards...
1

How each character of a String is represented in Java?

In Java, each character in a string is a 16-bit Unicode character. Because Unicode characters are 16 bits (not the skimpy 7 or 8 bits that ASCII provides), a rich, international set of characters is easily represented in Unicode.

2

Are strings object or primitive?

Strings are objects and like every other object you can create instance of a string using new keyword.

3

What are the constructors that can be used to create a string object?

String s = new String("abcdef");
String s = "abcdef";
What they have in common is that they all create a new String object, with a value of "abcdef", and assign it to a reference variable s

4

What happens when we concat a String?
String s = "abcdef";
s = s.concat(" more stuff");

The Java Virtual Machine (JVM) took the value of string s (which was "abcdef") and tacked " more stuff" onto the end, giving us the value "abcdef more stuff". Since strings are immutable, the JVM couldn't stuff this new value into the old String referenced by s, so it created a new String object, gave it the value "abcdef more stuff", and made s refer to it. At this point in our example, we
have two String objects: the first one we created, with the value "abcdef", and the second one with the value "abcdef more stuff". Technically there are now three String objects, because the literal argument to concat, " more stuff", is itself a new String object. But we have references only to "abcdef" (referenced by s2) and "abcdef more stuff" (referenced by s).

5

What is the output? For extra credit, how many String objects and how many reference variables were created prior to the println statement?

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);

Answer: The result of this code fragment is spring winter spring summer. There are two reference variables: s1 and s2. A total of eight String objects were created as follows: "spring ", "summer " (lost), "spring summer ", "fall "(lost), "spring fall " (lost), "spring summer spring " (lost), "winter "(lost), "spring winter " (at this point "spring " is lost). Only two of the eight String objects are not lost in this process.

6

How JVM uses String Constant Pool?

To make Java more memory efficient, the JVM sets aside a special area of memory called the String constant pool. When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created. (The existing String simply has an additional reference.)

7

Is it possible to override String methods and for example make it mutable?

String class is marked final. Nobody can override the
behaviours of any of the String methods.

8

What is the difference between these two string construction?
String s = "abc";
String s = new String("abc");

First one creates one String object and one. abc goes to pool and s references to it.
In second case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory and s will refer to it. In addition, the literal "abc" will be placed in the pool. It creates two objects and one reference variable.

9

What is difference between String length and array length?

Arrays have an attribute (not a method) called length. You may encounter questions in the exam that attempt to use the length() method on an array or that attempt to use the length attribute on a String. Both cause compiler errors-consider these, for example:
String x = "test";
System.out.println( x.length ); // compiler error
and
String[] x = new String[3];
System.out.println( x.length() ); // compiler error

10

What is the result?
String x = "0123456789";
System.out.println( x.substring(5) );
System.out.println( x.substring(5, 8));

First one is "56789"
Second one is "567"

11

When should one use StringBuilder class?

The java.lang.StringBuilder class should be used when you have to make a lot of modifications to strings of characters.A common use for StringBuilders is file I/O when large, ever-changing streams of input are being handled by the program. In these cases, large blocks
of characters are handled as units, and StringBuilder objects are the ideal way to handle a block of data, pass it on, and then reuse the same memory to handle the next block of data.

12

What is the difference between StringBuilder and StringBuffer?

Everything same apart from synchronization. StringBuffer's method are sync.ed so it's thread safe but slower.

13

What is the result?
StringBuilder sb = new StringBuilder("abc");
sb.append("def");
System.out.println("sb = " + sb);


StringBuilder sb = new StringBuilder("abc");
sb.append("def").reverse().insert(3, "---");
System.out.println( sb );

First one is "sb = abcdef" and second one is "fed---cba".
These StringBuilder methods operate on the value of the StringBuilder object invoking the method. So a call to sb.append("def"); is actually appending "def" to itself (StringBuilder sb).

14

What are the rules to keep in mind when appending and inserting to StringBuilder?

* If an append() grows a StringBuilder past its capacity, the capacity is updated automatically.
* If an insert() starts within a StringBuilder's capacity, but ends after the current capacity, the capacity is updated automatically.
* If an insert() attempts to start at an index after the StringBuilder's current length, an exception will be thrown.

15

What is the result?
StringBuilder sb2 = new StringBuilder("pi = ");
sb2.append(3.14159f);
System.out.println(sb2);

Output is "pi = 3.14159"

16

What is the result?
StringBuilder sb = new StringBuilder("0123456789");
System.out.println(sb.delete(4,6));

Output is "01236789"

17

What is the result?
StringBuilder sb = new StringBuilder("01234567");
sb.insert(4, "---");
System.out.println( sb );

Output is "0123---4567"

18

Where are arrays kept in Java?

Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements.

19

Which are legal array declarations?
int[] key;
int key [];
Thread[] threads;
Thread threads[];
String[][][] occupantName;
String[] managerName [];

All declarations are legal.

20

When JVM allocates space for array?

JVM allocates space for array when it is actually instantiated. That's when size matters.

21

Are there any Thread objects after this code executes?
Thread[] threads = new Thread[5];

Despite how the code appears, the Thread constructor is not being invoked. We're not creating a Thread instance, but rather a single Thread array object. After the preceding statement, there are still no actual Thread objects! The single object referenced by threads holds five Thread reference variables, but no Thread objects have been created or assigned to those references.

22

What is the result?
int[] carList = new int[];

Will not compile; needs a size

23

What does that code block tells?
int[][] myArray = new int[3][];

A two-dimensional array of type int is really an object of type int array (int []), with each element in that array holding a reference to another int array. Giving only first dimension size is OK since JVM needs to know only the size of the object assigned to the variable myArray.

24

What do below lines mean?
int[][] scores = new int[3][];
scores[0] = new int[4];
scores[1] = new int[6];
scores[2] = new int[1];

int[][] scores = new int[3][];
// Declare and create an array (scores) holding three references
// to int arrays
scores[0] = new int[4];
// the first element in the scores array is an int array
// of four int elements
scores[1] = new int[6];
// the second element is an int array of six int elements
scores[2] = new int[1];
// the third element is an int array of one int element

25

How do you get size of an array?

Arrays has a variable called length. It tells how many elements it holds but does not tell if the elements are ibitialized or not.

26

int[][] scores = {{5,2,4,7}, {9,2}, {3,4}};
How many objects are created in this statement?

4- one array of ints arrays and three int arrays.

27

Is this a legal array declaration?
new Object[3] {null, new Object(), new Object()};

No, size must not be specifed.

28

What values can be assigned to primitive arrays?

Primitive arrays can accept any value that can be promoted implicitly to the declared type of the array.

29

Are those declarations valid?
int[] weightList = new int[5];
byte b = 4;
char c = 'c';
short s = 7;
weightList[0] = b;
weightList[1] = c;
weightList[2] = s;

Yes,
int[] weightList = new int[5];
byte b = 4;
char c = 'c';
short s = 7;
weightList[0] = b; // OK, byte is smaller than int
weightList[1] = c; // OK, char is smaller than int
weightList[2] = s; // OK, short is smaller than int

30

What values can be asssigned to object reference arrays?

If the declared array type is a class, you can put objects of any subclass of the declared type into the array.
If the array is declared as an interface type, the array elements can refer to any instance of any class that implements the declared interface.
The bottom line is this: Any object that passes the IS-A test for the declared array type can be assigned to an element of that array.