0

Arrays and Strings

Description: Arrays and Strings
Number of Questions: 15
Created by:
Tags: Arrays and Strings Java/Oracle /C/C++ Java
Attempted 0/15 Correct 0 Score 0

How many string objects are created by the given statement? String str=new String(“Aliens”);

  1. 0

  2. 1

  3. 2

  4. 4


Correct Option: C
Explanation:

The given statement creates 2 objects. The first object is refereed to by “str” and the second by the last string “Aliens”.

What will be the value of “str” after executing the following statements? StringBuffer str=new StringBuffer(“I am a student”); str.delete(2,6);

  1. I student

  2. Istudent

  3. Itudent

  4. I am a student


Correct Option: A
Explanation:

The method str.delete() deletes the characters in the string in the sequence 2 to 6. Hence, the output will be “I student”.

Which of the following statements is/are correct? A. The size of a vector can grow or shrink. B. The size of an array can grow or shrink.

  1. Only A

  2. Only B

  3. Both A and B

  4. Neither A nor B


Correct Option: A
Explanation:

The size of an array cannot grow or shrink but the size of a vector can grow or shrink.

What is the size and capacity of the Vector Num in the following code? Vector Num=new Vector(10)

  1. Size = 10, capacity = 0

  2. Size = 0, capacity = 10

  3. Size = 10, capacity = 10

  4. Size = 0, capacity = 0


Correct Option: B
Explanation:

The number of elements in the vector represents the size of the vector and the maximum number of elements stored in the vector is the capacity of the vector that is 10 in this case.

Which of the following methods is used to convert a given string to a new character array?

  1. toArray()

  2. StringToArray()

  3. toCharArray()

  4. toCArray()


Correct Option: C
Explanation:

The toCharArray() method is used to convert a given string to a new character array. Its return type is character array. Syntax: char[] toCharArray();

Which of the following methods is used to check whether the stream is ready to read or not?

  1. start()

  2. ready()

  3. isready()

  4. isstart()


Correct Option: B
Explanation:

The ready() method is used to check whether the stream is ready to read.

What would be the value of i after the execution of the following statements in Java? int i; String st1=”Rakesh”; String st2=”Roshan”; i=st1.compareTo(st2);

  1. 0

  2. 1

  3. -1

  4. +1


Correct Option: C
Explanation:

Here, the output will be -1 because the ASCII difference of unmatched characters in “Rakesh” and “Roshan” is negative.

What does the startIndex and endIndex argument in delete() method of StringBuffer class specify?

  1. Deletion of characters from startIndex position to endIndex position

  2. Deletion of characters from startIndex position to endIndex-1 position

  3. Deletion of all the characters

  4. Deletion of the startIndex and endIndex characters only

  5. Deletion of a single character only


Correct Option: B
Explanation:

This is the correct because the number of characters are deleted upto endIndex-1.

Which of the following is the correct way to read a character?

  1. char ch;

    BufferedReader br=new BufferedReader(new InputStreamReader(System.out)); ch=(char)br.read();|

  2. char ch;

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); ch=br.read();|

  3. char ch;

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); ch=(char)br.read();|

  4. char ch;

    BufferedReader br=new BufferedReader(System.in); ch=(char)br.read();|


Correct Option: C
Explanation:

The steps to read a character are as follows. char ch; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); ch=(char)br.read();

What would be the value of i and ch after the execution of the following statements in Java? int i; char ch; String st1=”Rakesh Roshan”; i=st1.indexOf(‘e’); ch=st1.charAt(5);

  1. The value of i will be 4 and the value of ch will be ’s’.

  2. The value of i will be 3 and the value of ch will be ’h’.

  3. The value of i will be 3 and the value of ch will be ’s’.

  4. The value of i will be 4 and the value of ch will be ’h’.


Correct Option: B
Explanation:

The index of character ‘e’ is 3 because the index is starting from 0. The character at position 5 is ‘h'.

What will be the output of the program? class PassS { public static void main(String [] args) { PassS p = new PassS(); p.start(); }

void start() { String s1 = "slip"; String s2 = fix(s1); System.out.println(s1 + " " + s2); }

String fix(String s1) { s1 = s1 + "stream"; System.out.print(s1 + " "); return "stream"; } }

  1. slip stream

  2. slipstream stream

  3. stream slip stream

  4. slipstream slip stream


Correct Option: D
Explanation:

When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of "slip"). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of "slipstream"). When the program returns to start(), another String object is created, referred to by s2 and with a value of "stream".

The method readLine() can be accessed by the object of a ____________ class to read a string from console.

  1. InputStreamReader

  2. InputStream

  3. BufferedReader

  4. StringReader


Correct Option: C
Explanation:

The readLine() method can be accessed by the object of a BufferedReader class. For example,  String str; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); str=br.readLine();

What is the correct syntax of replace() method of the StringBuffer class?

  1. StringBuffer replace(int s,int e)

  2. StringBuffer replace(int s,String str)

  3. StringBuffer replace(int s,String str1,String str2)

  4. StringBuffer replace(int s,int e,String str)


Correct Option: D
Explanation:

The correct syntax of the replace() method is StringBuffer replace(int s,int e,String str) Replaces the characters in a substring of the given sequence with the given string 'str'.

The methods of NumberFormat class are used to display the numbers in standard formats(Currency values and percentage values).The NumberFormat class belongs to which of the following packages?

  1. java.lang

  2. java.io

  3. java.text

  4. java.txt


Correct Option: C
Explanation:

The NumberFormat class belongs to java.text package. It has three methods that yield standard formats for numbers, currency values and percentage values.

Which of the following statement(s) is/are correct? A. valueOf() is a static method. B. toString() is overridden in Integer and Double Class.

  1. Only A

  2. Only B

  3. Both A and B

  4. Neither A nor B


Correct Option: C
Explanation:

Both the statements are correct. The methods valueOf() and toString() belong to String class. The valueOf() method is a static method, so it is always called by using the class name String and the method toString() is overridden in wrapper classes, such as Integer and Double.

- Hide questions