Test Your Understanding - Arrays and Strings - Attempt Review
Test Your Understanding - Arrays and Strings - Attempt Review
Dashboard / My courses / Java / Arrays and Strings / Test Your Understanding - Arrays and Strings
Quiz review
Started on Monday, 20 May 2024, 10:14 PM
State Finished
Completed on Monday, 20 May 2024, 10:28 PM
Time taken 14 mins 4 secs
Marks 18.00/21.00
Grade 85.71 out of 100.00
Feedback Congratulations!!! You have passed by securing more than 80%
Question 1
Correct
class String_demo
53051
{
System.out.println(s);
}
}
53051
Select one:
a. c
b. abc
c. a
d. b
53051
A character array is initialized with 'a', 'b' and 'c' and the array reference is chars. Printing this reference will output abc.
A "new" string object is initialized with this reference and this object is referred by "s". Printing this reference will output abc.
The correct answer is: abc
Question 2
Correct
int arr[4]={};
System.out.print(arr[0]);
53051
Select one:
a. Runtime error
b. 0
c. Garbage error
53051
53051
Question 3
Correct
}
}
53051
Select one:
a. The program has a runtime error because the array element x[0] is not defined.
b. The program has a compile error because the size of the array wasn't specified when declaring the array.
c. The program has a runtime error because the array elements are not initialized.
The "new" keyword allows memory for storing integer elements in an array to be created in the "heap" and the memory is initialized with "default of integer"
which is 0.
The correct answer is: The program runs fine and displays x[0] is 0.
53051
53051
Question 4
Correct
class array_output
{
{
array_variable[i] = 'i';
System.out.print(array_variable[i] + "");
53051
}
}
}
Select one:
a. 0 1 2 3 4 5 6 7 8 9 10
b. i j k l m n o p q r
c. i i i i i i i i i i
d. 1 2 3 4 5 6 7 8 9 10
53051
array_variable is a character array that can hold 10 characters. The for loop gets iterated for 10 times. During each iteration, the array index is assigned with
the character "i" and printed alongside. Hence the output "iiiiiiiiii".
53051
Question 5
Correct
String s1 = “one”;
String s2 = s1.concat(“two”);
Select one:
a. onetwo
b. twoone
53051
c. one
d. two
The string "two" referred by s2 is "concatenated to" the string "one" referred by s1.
Question 6
Correct
53051
Mark 1.00 out of 1.00
Select one:
a. alloc
b. calloc
c. new
d. malloc
Question 7
Correct
Select one:
True
False
Question 8
Correct
53051
+ operator can be used to concatenate two or more String objects in java. State true or false.
Select one:
True
False
53051
53051
Question 9
Correct
What will be the content of array variable table after executing the following code?
{
public static void main(String[] args)
53051
if(j == i)
{
table[i][j] = 1;
System.out.print(table[i][j]);
}
else
table[i][j] = 0;
System.out.print(table[i][j]);
}
53051
System.out.println("\n");
Select one:
a.
Compilation error
b.
100 53051
110
111
c.
000
000
000
d.
100
010
001
"table" is a 2 dimensional array with 5 rows and 5 columns. It is iterated from 0 through 3 and during each iteration j==i (iteration variables) is
checked. When j==i evaluates to true, the index is assigned the value "1" and printed. Else, assigned "0'" and printed.
Question 10
Correct
Select one:
a. length
b. size()
c. len
53051
d. length()
Question 11
Correct
53051
Your answer is correct.
The correct answer is:
Given a one dimensional array arr, what is the correct way of getting the number of elements in arr is [arr.length]
Question 12
Correct
53051
Question 13
Incorrect
{
53051
int[] x = {1, 2, 3, 4};
int[] y = x;
x = new int[2];
Select one:
53051
a. 1 2
b. 0 0
c. 1 2 3 4
d. 0 0 0 0
Array x is initialized with 4 values and this means reference "x" contains the starting address of the array. This address is copied to the array reference "y".
This mean the 4 values can now be accessed with "y" as well. Then the reference x is assigned with a new array's starting address whose length is 2. Hence
the iteration outputs 1 2
Question 14
Correct
public class A
{
public static void main(String argv[])
53051
Select one:
a. 2
b. 1
The array ary is initialized with 3 elements and the element at the first index is 2.
The correct answer is: 2
53051
53051
Question 15
Correct
class String_demo
System.out.println(s);
}
53051
Select one:
a. ABC
b. BCD
c. ABCD
d. CDA
53051
An integer array is initialized with values 65, 66, 67 and 68. Its reference is "ascii". A new string object is initialized with this reference
such that the elements from index 1 through 3 alone gets copied as "characters". This object is referred by "s". Printing this object will
output BCD which are the char-equivalents of 66, 67 and 68.
The correct answer is: BCD
53051
Question 16
Incorrect
Given:
1. public class MyLogger {
2. private StringBuilder logger = new StringBuuilder();
3. public void log(String message, String user) {
4. logger.append(message);
5. logger.append(user);
6. }
7. }
The programmer must guarantee that a single MyLogger object works properly for a multi-threaded system.
How must this code be changed to be thread-safe?
Select one:
a. Replace StringBuilder with just a String object and use the string concatenation (+=) within the log method.
53051
b. No change is necessary, the current MyLogger code is already thread-safe.
StringBuffer is synchronized and therefore thread-safe. StringBuilder is compatible with StringBuffer API but with no guarantee of
synchronization. Because it’s not a thread-safe implementation, it is faster and it is recommended to be used only in places where
there’s no need for thread safety.
The correct answer is: Replace StringBuilder with StringBuffer
Question 17 53051
Partially correct
What is special about string objects as compared to objects of other derived types?
53051
b. Java provides string constant pool to store the string objects
c. You can create string objects without or without using new operator
Question 18
Partially correct
Fill in appropriately.
Note : In heap memory does not include String pool, though pool is inside heap.
The "new" keyword allows memory for storing String to be allocated in the "heap" . Otherwise, memory is allocated in string pool.
53051
The correct answer is:
Fill in appropriately.
String st1 = new String("JAVA");
Note : In heap memory does not include String pool, though pool is inside heap.
Question 19
53051
Correct
Select one:
a. hell
53051
b. Hel
c. ello
d. llo
Question 20
Correct
class Output
{
public static void main(String args[])
{
}
}
53051
Select one:
a. 5 10
b. 10 5
c. 0 5
d. 0 10
53051
53051
Question 21
Correct
class Evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
53051
}
Select one:
a. 1
b. 6
c. 0
d. 3
53051
When n is initialized with value 6, n = arr[arr[n] / 2] evaluates to n = 3. Now, printing arr[n] / 2 will output 1.
◄ String
Jump to...
53051