0

Java

Description: Concept based questions
Number of Questions: 15
Created by:
Tags: Concept based questions Java Basics
Attempted 0/15 Correct 0 Score 0

How many constructors are there of ArrayList class in Java?

  1. 6

  2. 5

  3. 4

  4. 3

  5. 2


Correct Option: D
Explanation:

This is the correct answer as there are 3 constructors of ArrayList class in Java Collections, viz. ArrayList(): creates an empty constructor with default initial capacity; ArrayList(c : collection): creates an arraylist from existing collection c; and ArrayList(x int): creates an arraylist with initial capacity x. So, this is the correct answer.

Which of the following methods is present in the Stack class in Java Collections framework?

  1. remove()

  2. element()

  3. Pop()

  4. poll()

  5. exit()


Correct Option: C
Explanation:

This is the correct answer because pop() method returns and removes the top element of the stack. So, this is the correct answer.

Which of the following methods is present in LinkedList class in Java Collections?

  1. addFirst()

  2. setFirst()

  3. setLast()

  4. indexOf(e:Element)

  5. lastIndexOf(e:element)


Correct Option: A
Explanation:

This is the correct answer. addFirst() method is used to add the element to the head of the list which is specified in its parameter and this method is present in LinkedList class. So, this is the correct choice.

Which of the following methods is/are static in Collections class?

  1. Frequency(c : collection, o : object)

  2. Reverse(List)

  3. Sort(List)

  4. Both (1) and (2)

  5. All of the above


Correct Option: E
Explanation:

As I explained that all the methods given in the options are static and are present in Collections class. So, this is the correct choice.

Which of the following methods are present in the queue interface?

  1. poll()

  2. search(o:Object)

  3. empty()

  4. exit()

  5. pop()


Correct Option: A
Explanation:

This is the correct choice. poll() method is present in queue interface and it returns and removes the head of the queue and returns null if empty.  

Which of the following methods is/are not present in Vector class in Java Collections framework?

  1. elementAt(index)

  2. firstElement()

  3. removeElementAt(int)

  4. setSize(int)

  5. removeFirst()


Correct Option: E
Explanation:

This is the correct choice as removeFirst() method is not present in Vector class; rather it is present in LinkedList class and is used to remove the first element of the list.  

What is the initial capacity of vector?

  1. 16

  2. 10

  3. 20

  4. 12

  5. 18


Correct Option: B
Explanation:

This is the correct choice because the default constructor of Vector class will create a vector with a default initial capacity 10. Vector v1 = new Vector(): This will create a vector with initial capacity 10. You can check this by v1.capacity(). So, this is the correct answer.

Which of the following methods in Collections class is not static?

  1. Min(c : collection)

  2. Shuffle(list)

  3. Frequency(c : collection, o : object)

  4. Copy(destination, source)

  5. Merge(c : collection, c1 : collection)


Correct Option: E
Explanation:

This is the correct answer because there is no method named Merge(c : collection, c1 : collection) in Collections class.  

Which of the following methods is present in ListIterator interface in Java Collections?

  1. nextIndex()

  2. addAll()

  3. IndexOf()

  4. listIterator()

  5. subList(startindex,endindex)


Correct Option: A
Explanation:

This is the correct answer. nextIndex() method is present in the ListIterator interface in Java Collections. This method returns the index of the next element in the list to be traversed. So, this is the correct answer.

Which of the following methods is/are defined in String class for comparing strings?

  1. equals(String s)

  2. compareToIgnoreCase(String s1)

  3. compareTo(String s3)

  4. Both (2) and (3)

  5. All of the above


Correct Option: E
Explanation:

As all of the functions are defined in the String class, so this is the correct answer.

How many constructors are there of Vector class in Java Collections framework?

  1. 6

  2. 5

  3. 4

  4. 3

  5. 2


Correct Option: C
Explanation:

This is the correct answer. There are 4 constructors of Vector class, viz. Vector(): creates a default vector with initial capacity 10; Vector(c : collections): creates a vector from existing collection; Vector(initialcapacity int): creates a vector with specified initial capacity; and Vector(initialcapacity int,increment int): creates a vector with specified initial capacity and increment. So, this is the correct answer.

Which of the following methods is present in Vector class in Java Collections framework?

  1. removeAllElements()

  2. element()

  3. removeLast()

  4. get(index : int)

  5. set(index,element)


Correct Option: A
Explanation:

This is the correct choice. removeAllElements() method is present in Vector class and is used to remove all the elements from the vector.  

What will be the output of the given code?

String s = “Welcome To Java” s.substring(4, 4);

  1. o

  2. c

  3. Empty string

  4. Compile time error

  5. Run time error


Correct Option: C
Explanation:

This is the correct answer. Substring method takes two parameters (BeginIndex, EndIndex).Substring(int Begin, int End). It returns the string from the beginning to end – 1. But if the BeginIndex is equal to the EndIndex, then an empty String with length 0 will be returned. So, here substring(4, 4) will return an empty string of length 0.  

What will be the result of the following code?

String s = “Welcome To Java”; s.charAt(s.length());

  1. a

  2. v

  3. Compiler Error : cannot pass a function as parameter

  4. Compiler Error : parameters not sufficient

  5. Run time error


Correct Option: E
Explanation:

This is the correct answer. s.length() will return 15 because there are 15 characters in the string. So, the code looks like this: s.charAt(15); the index count starts at 0. In this way, the last character is 'a' at index number 14. So, if we try to access a character in a string which is out of bound, then a runtime exception “StringIndexOutOfBound” will be thrown. So, we can pass only till s.charAt(s.length()-1).

What will be the output of the given program? class BitShift { public static void main(String [] args) { int x = 0x80000000; System.out. print(x + and) ; x = x >>> 31; System.out. println(x); } }

  1. -2147483648 and 1

  2. 0x80000000 and 0x00000001

  3. -2147483648 and -1

  4. 1 and -2147483648

  5. None of these


Correct Option: A
Explanation:

Option (1) is correct. The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation looks like this: Before: 1000 0000 0000 0000 0000 0000 0000 0000 After: 0000 0000 0000 0000 0000 0000 0000 0001 Converting this into decimal, the result will be 2147483648 and 1 So, this is the correct choice.

- Hide questions