0

Java Basics (GATE - CS)

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

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

What will be the result of the above code?

  1. ome

    • 1
  2. com

  3. Runtime error

  4. Compile time error


Correct Option: D
Explanation:

This is the correct answer. Substring method takes two parameters (beginIndex,EndIndex). Substring( int Begin , int End). It returns the string from begin to end -1. But if the begin index is greater than the end index then a runtime error will occur. So here substring(4,3) will give a runtime error.

Which of the following functions cannot be invoked using the object of String class?

  1. toLower()

  2. trim()

  3. split()

  4. Both (1) and (3)

  5. Both (2) and (3)


Correct Option: A
Explanation:

This is the correct answer. Because there is no method defined in the String class named toLower(). So it cannot be invoked using the object of String class. The correct method is toLowerCase() which converts the characters to lowercase.

String s1 = “abc”; String s2 = “abg”; S1.compareTo(s2);

What will be the result of the above code?

    • 4
  1. 4

    • 1
  2. 1

  3. 0


Correct Option: A
Explanation:

This is the correct answer. The first two characters from s1 and s2 (a vs a) are compared and they are equal. Then (b vs b) are compared and they are also equal.  Now (c vs g) are compared. Since c is 4 less than g, so - 4 is returned.  

Which of the following methods is/are provided by the character class?

  1. isDigit()

  2. isLetter()

  3. isLetterOrDigit()

  4. All of the above

  5. None of these


Correct Option: D
Explanation:

All of the methods given are defined in the character class. So this is the correct choice.

String s = “Welcome To Java” s.lastIndexOf(“Java”,5);

What is the result of the given statements?

    • 1
  1. 11

  2. 6

  3. Compiler error : method lastIndexOf() not declared

  4. Compiler error : parameters to method not sufficient


Correct Option: A
Explanation:

lastIndexOf(String s, index i) method return the last occurrence of string "s" before the index value i. it Return -1 if string not found. So here the string to find is “Java” before the index value 5 which means before the character "m". but string “java” is not present before the index 5 i.e character "m" .so it will return -1. So this is the correct answer.

String s = “Welcome To Java”; String s1 = “Java”; s.match(s1);

What will be the output of the given piece of code?

  1. True

  2. False

    • 1
  3. Compile time error

  4. Runtime error


Correct Option: D
Explanation:

This is the correct answer. The compiler will throw an error that method match() was not declared. Because String class does not contain any method named match(). The correct method is matches(). So the above statement should be s.matches(s1). So this is the correct answer. The program will not run. So run time error cannot occur because a compiler error is there. So this is wrong.

Which of the following functions is/are valid in Java for Strings?

  1. valueOf()

  2. toCharArray()

  3. toIntArray()

  4. Both (1) and (2)

  5. Both (1) and (3)


Correct Option: D
Explanation:

Both 1 & 2 options are valid functions in Java. So this is the correct answer.

StringBuffer s = new StringBuffer(“Welcome To Java”); s.delete(8,12);

What will be the output of the given code segment?

  1. Welcome ava

  2. Welcome va

  3. Welcomeava

  4. Compiler error : delete cannot be used with StringBuffer

  5. Runtime error


Correct Option: A
Explanation:

This is the correct answer. delete(int start , int end) method deletes the characters from the start index until  the end -1 index. So, the welcome to java will delete the characters from 8 to 11 characters. Therefore, the resulting string remains “ Welcome ava”. The index starts from 0.

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

What will be the answer of the given code?

  1. o

  2. c

  3. Empty string

  4. Compile time error

  5. Runtime 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 begin to end – 1. But if the begin index is equal to the end index then an empty String with length 0 will be returned. So here substring(4,4) will return an empty string of length 0. So this is the correct answer.

Which of the following data types can be passed as the first parameter to the lastIndexOf() method in String class?

  1. Char

  2. String

  3. Int

  4. Both (1) and (2)

  5. All of the above


Correct Option: D
Explanation:

Both char and string can be passed as the first parameter to the lastIndexOf() method. So this is the correct answer.

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

What will be the output of the given code?

  1. s.length() = 14s.charAt(9) = T

  2. s.length() = 15s.charAt(9) = o

  3. s.length() = 15s.charAt(9) = T

  4. s.length() = 14s.charAt(9) = o

  5. Compiler error


Correct Option: B
Explanation:

This is the correct answer. Length() function returns the number of characters in the string. The number of characters in the given string is 15. So length() will return 15.charAt() function return the character at the given index in the parameter. Remember that the index count starts at 0. So charAt(0) is W. Counting in this way we reach charAt(9) is o. So this is the correct answer.

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

What will be the result of the above code?

  1. a

  2. v

  3. Compiler Error : cannot pass a function as a parameter

  4. Compiler Error : parameters are not sufficient

  5. Runtime error


Correct Option: E
Explanation:

This is the correct answer. S.length() wil 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 a is at index number 14. So if we try to access "a" character in a string which is out of bounds then a runtime exception “StringOutOfBound” will be thrown. So we can pass only until s.charAt(s.length()-1).  So this is a correct answer.

Which of the following methods cannot be used with the object of String class?

  1. replace(char, char)

  2. replaceAll(String, String)

  3. append(String)

  4. Both (1) and (2)

  5. All of these


Correct Option: C
Explanation:

This is the correct answer. Because the String class objects are immutable (i.e once the object of the String class is created its contents cannot be changed). So we cannot append new characters or string in the old string. So this is the correct answer.

StringBuffer s = new StringBuffer(“Hello”); s.capacity();

What will be returned by the function capacity()?

  1. 16

  2. 4

  3. 18

  4. 3

  5. 15


Correct Option: A
Explanation:

The correct answer is 16. When stringbuffer object is created the default capacity of the object is always 16. If the number of characters in the string increases, the capacity will increase with the following formula:-2 * (previous capacity + 1). In this case hello has 5 characters, so the default capacity will not change. Hence, this is correct.

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

  1. equals(String s)

  2. compareToIgnoreCase(String s1)

  3. compareTo(String s3)

  4. Both (2) and (3)

  5. All of these


Correct Option: E
Explanation:

All of the functions are defined in the String class. So this is correct answer.

- Hide questions