Write the answers and give explaination of each answer: 1. Which of these is an
ID: 3593668 • Letter: W
Question
Write the answers and give explaination of each answer:
1. Which of these is an incorrect array declaration?
a) intarr[]=newint[5] b) int [] arr = new int[5] c) int arr[] = new int[5] d) int arr[] = int [5] new
Answer :_______
Explaination: ___________
2. What will this code print?
int arr[] = new int [5];
System.out.print(arr);
a) 0 b) value stored in arr[0] c) 00000 d) Class name@hashcode in hexadecimalform
Answer :_______
Explaination: ___________
3. What is the output of this program?
class array_output {
public static void main(String args[]) {
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = i;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
a) 0 2 4 6 8 b) 1 3 5 7 9 c) 0 1 2 3 4 5 6 7 8 9 d) 1 2 3 4 5 6 7 8 9 10
Answer :_______
Explaination: ___________
4. Look at the following code.
Integer myNumber = new Integer(5);
int var = myNumber;
What is true about the second statement?
a) The statement performs autoboxing.
b) It results in an error because you can’t assign a wrapper class to a primitive variable.
c) The statement performs unboxing.
d) The statement performs unwrapping.
Answer :_______
Explaination: ___________
5. Which is the valid declaration within an interface definition?
a) public double methoda(); b) public final double methoda();
c) static void methoda(double d1); d) protected void methoda(double d1);
Answer :_______
Explaination: ___________
6. True or False? A private base method can be overridden by some derived public method.
Answer :_______
Explaination: ___________
7. Which of these keywords is not a part of exception handling?
a) try b) finally c) thrown d) catch
Answer :_______
Explaination: ___________
8. What line of code should replace the missing statement to make this program compile?
/* Missing Statement ? */
public class foo
{
public static void main(String[]args)throws Exception
{
java.io.PrintWriter out = new java.io.PrintWriter();
new java.io.OutputStreamWriter(System.out);
out.println("Hello");
}
}
a) No statement required. b) import java.io.*;
c) include java.io.*; d) import java.io.PrintWriter;
Answer :_______
Explaination: ___________
9. The process of converting one date type to another is called__________.
Translating b) Casting c) Compiling d) Declaring
Answer :_______
Explaination: ___________
10. Which of the following is the correct declaration statement in java program?
a) int num=int[5] b) int num=new num[5]
c) int[] num=new int[5] d) None
Answer :_______
Explaination: ___________
11. ____________method is used to find the nth no. of character of given string s1.
s1.index(n) b) s1.substring(n) c) s1.length() d) s1.charAt(n)
Answer :_______
Explaination: ___________
12. Which of these can be overloaded?
a) Methods b) Constructors c) All of the above d) None of the above
Answer :_______
Explaination: ___________
13. The concept of multiple inheritance is implemented in Java by?
I. Extending two or more classes.
II. Extending one class and implementing one or more interfaces.
III. Implementing two or more interfaces.
a) Only (II) b) (I) and (II) c) (II) and (III) d) Only (I) e) Only (III)
Answer :_______
Explaination: ___________
14. True or false? Private members of a class are inherited in the sub class.
Answer :_______
Explaination: ___________
15. True or false? A method of super class with a default access modifier can be overridden as protected or public but not as private.
Answer :_______
Explaination: ___________
16. What is the output of following linked list li?
ll.add("F");
ll.add("B");
ll.add("D");
ll.add("E");
ll.add("C");
ll.addLast("Z");
ll.addFirst("A");
ll.add(1, "A2");
a) [A, A2, F, B, D, E, C, Z] b) [ Z, C, E, D, B, F, A2, A]
c) [F, B, F, D, E, C, Z, A, 1 A2] d) [ A, 1 A2, F, B, D, E, C, Z]
Answer :_______
Explaination: ___________
17. What will be the output of the following program?
class X {
void method(int a) {
System.out.println("ONE");
}
void method(double d) {
System.out.println("TWO");
}
}
class Y extends X {
@Override
void method(double d) {
System.out.println("THREE");
}
}
public class MainClass {
public static void main(String[] args) {
new Y().method(100);
}
}
a) ONE b) TWO c) THREE d) ONE TWO e) Error
Answer :_______
Explaination: ___________
Coding and Definitions:
18. What is the difference between ArrayList and LinkedList?
19. What is the difference between Comparable and Comparator?
20. What is the difference between Queue and Stack?
21. What is difference between abstract class and interface?
22. Define Polymorphism?
23. Write minimum of 4 data types supported in java?
24. Use any sorting method to sort an array list in ascending order? (DO NOT USE ANY PRE DEFINED METHODS)
25. You have a queue with 10 integers Q = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }. The expected output is
Q = { 1, 6, 2, 7, 3, 8, 4, 9, 5, 10 }. Fill in the blanks alongside comments where shown to show trace results of the stack or queue.
public class InterleavingElements {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<Integer>();
for (int i = 1; i <= 10; i++)
q.add(i); //1,2,3,4,5,6,7,8,9,10
InterleavingElements ii = new InterleavingElements();
ii.Interchange(q);
System.out.println(q);
}
public Queue<Integer> Interchange(Queue<Integer> q){
Stack<Integer> st = new Stack<Integer>();
int size = q.size()/2;
for(int i = 1; i <= size; i++)
st.push(q.remove()); //1.______________
while(!st.isEmpty())
q.add(st.pop()); //2. _____________
for(int i = 1; i <= size; i++)
q.add(q.remove()); //3. _____________
for(int i = 1; i <= size; i++)
st.push(q.remove()); //4. _____________
while(!st.isEmpty()){
q.add(st.pop());
q.add(q.remove()); // 5. _____________
}
return q;
}
}
26. Create and define a method called popList that gets passed an arraylist called list that will accept any numeric value type passed in. The method definition should assign 5 integers to the arraylist via a loop storing the values of 1,2,3,4 and 5 respectively.
Answer :_______
Explaination: ___________
Explanation / Answer
1 Answer) d) int arr[] = int [5] new
Explanation:- In java arrays are initialised same as like objects and initialised with the new keyword
2 Answer) d) Class name@hashcode in hexadecimalform
Explanation:- arr is the reference to the array and when you print its value its hash code will be printed
3 Answer) a) 0 2 4 6 8
Explanation:- the loop variable is incremented two times once in the for loop and once in the loop code.
4 Answer) d) The statement performs unwrapping.
Explanation:- when we assign a Wrapper object to a primitive data type the compiler will perform auto unwrapping
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.