Data Structures in Java
Data Structures in Java
ArrayTest.insert(arr, 4, 'J');
for(int index=0;index<arr.length;index++)
System.out.println(arr[index]);
}
}
ArrayTest class is given below.
class ArrayTest {
for(int index=ar.length-1;index>=pos;index--) {
ar[index]=ar[index-1];
}
ar[pos-1]=val;
}
}
ASDJ
Error: ArrayIndexOutOfBoundsException
ASDFJ
ASDJF
Q2 of 20outlined_flag
What will be the output of the code given below?
Assumption: LinkedList class is already implemented with all the required methods.
public class Tester {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.addAtEnd("11");
list.addAtEnd("13");
list.addAtEnd("18");
list.addAtEnd("34");
list.addAtEnd("46");
operate(list);
list.display();
}
public static void operate(LinkedList list ) {
Node temp = list.getHead();
while (temp.getNext().getNext() != null) {
temp.setData(temp.getNext().getData());
temp = temp.getNext();
}
}
}
11->13->18->34->46
13->13->34->34->46
11->13->18->34->34
13->18->34->34->46
Q3 of 20outlined_flag
What will be the output of the code given below?
Assumption: Stack class is already implemented with all the required methods.
public class Tester {
public static void main(String args[]) {
Stack stack = new Stack(10);
stack.push(18);
stack.push(10);
stack.push(24);
stack.push(56);
stack.push(27);
operate(stack);
stack.display();
}
}
75
20
51
Q5 of 20outlined_flag
What will be the output of the code given below?
Assumption: Queue class is already implemented with all the required methods.
public class Tester {
public static void main(String args[]) {
Queue queue = new Queue(10);
operate(queue);
queue.display();
}
}
}
}
}
}
}
A B C E D null
A B C D null E
A B C C E D E null E
Compilation error as null cannot be added
Q11 of 20outlined_flag
What will be the output of the code given below?
public class Tester {
public static void main(String[] args) {
Set<String> treeSet = new TreeSet<String>();
treeSet.add(new String("A"));
treeSet.add(new String("B"));
treeSet.add(new String("C"));
treeSet.add(new String("C"));
treeSet.add(new String("E"));
treeSet.add(new String("D"));
treeSet.add(new String("a"));
treeSet.add(new String("F"));
Object[] elements = treeSet.toArray();
for (Object element : elements)
System.out.print(element + " ");
}
}
AaBCDEF
ABCCEDaF
ABCEDaF
ABCDEFa
Q12 of 20outlined_flag
Choose the correct option based on the execution of the code given below.
public class Tester {
public static void main(String args[]) {
Map<String, Integer> studentDetails = new HashMap<String,
Integer>();
studentDetails.put("Max", 337);
studentDetails.put("Stocks", 480);
studentDetails.put("Malinda", 570);
studentDetails.put("Mathew", 640);
studentDetails.put("Max", 340);
if (studentDetails.replace("stocks", 480, 650)) {
studentDetails.remove("Max");
} else {
studentDetails.put("Sam", 490);
}
System.out.println(studentDetails);
}
Big Theta
Big Omega
Big Oh
O(n)
O(sqrt(n))
O(n/2)
O(log n)
Q17 of 20outlined_flag
What is the best-suited condition for using linear search algorithm?
4
Q19 of 20outlined_flag
What is the time complexity of bubble sort algorithm?
O(n*n)
O(n*log n)
O(n)
O(1)
Q20 of 20outlined_flag
Which of the given algorithmic approach tries to achieve a localized optimum solution?
Dynamic programming
Brute force
Greedy approach