Java: 1. What is the difference between a shallow and a deep copy? 2. What is a
ID: 3576928 • Letter: J
Question
Java:
1. What is the difference between a shallow and a deep copy?
2. What is a StringTokenizer used for?
3. What is a “Wrapper” class and why are wrapper classes used?
4. What is an array, how is it declared, and how is it created?
5. What is a ragged array?
7. What does the modifier static do when describing a variable? a method?
8. What is the difference between call-by-value and call-by-reference?
9. What is a privacy leak?
10. What is the purpose of the Object class, its relation to other classes, some of the things it provides to other classes?
11. How is the “new-and-improved” version of equals() differ from the other versions we’ve used so far?
12. What is the difference between a selection, an insertion, and a bubble sort?
Explanation / Answer
1. Shallow copy copies all the fields of an object and If original object has any references to other objects then only references of those objects are copied into clone object, and no copy of those objects are created.
In deep copy the referenced objects are also copied along with all the fields of the original object.
2. StringTokenizer class is used to break a string into tokens based on a delimiter
e:g StringTokenizer stkz = new StringTokenizer("my name is Asif"," "); breaks the string based on spaces
3. Wrapper class is used to represent or use primitive data types like int, double etc with Objects whenever required. Process of converting primitive to Object is called Boxing
4. An array is a collection of contigeous homogeneous data items.
Declaration dataType[] name;
Creation name = new dataType[size];
e:g int[] arr = new int[10] will create integer array of 10 elements
5. Ragged array or jagged array is a multi dimensional array in which each row can be of different size i:e each row can have variable number of columns.
e:g
int arr[][] = new int[2][]; //Declare 2d array
// First row has 3 columns
arr[0] = new int[3]; // Second row has 2 columns
arr[1] = new int[2];
7. static keyword makes a variable global which means this variable belongs to class not objects and its value will be shared between all instances of that class.
8. In call by value the value is passed directly i:e the value gets copied into the method and any changes made in the method remains in the method. While in pass by reference the reference (or in other words address ) of a variable is passed and any changes made to this variable is reflected in the calling program.
9. When a private member of a class is returned from a member method, the private member gets exposed to the outside class for example
public class A{
…
public void m() {…};
}
public class B{
private A q
= new A();
…
public A getA(){
return q;
}
This is called Privacy Leak. and to avoid this one should always return copy of object as
A getA(){
return new A(q);
}
10. Object class is the parent class of all the classes in java. Any object in java implicitly inherits from Object class.
12.
It has best, average and worst complexity of O(n), O(n^2) and O(n^2) resp
Selection Sort Insertion sort Bubble sort Selection sort scans from first element and compares the value with all other elements and marks the lowes element in the list and then replaces the element with the lowest value. This way the lowest value comes at first place in the list.Similarly, starting from second element and perform the same operation Insertion sort Insertion sort is a sorting algorithm that works the way a player sorts his playing cards in hand. Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. It starts from first element and compares with next element and swaps them if first element is greater than second, and then move to next element and so on It has best, average and worst complexity of O(n^2)
It has best, average and worst complexity of O(n), O(n^2) and O(n^2) resp
It has best, average and worst complexity of O(n), O(n^2) and O(n^2) respRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.