Java Basics
Java Basics - Important Points
21. | Which of the following is a valid way to declare an array in Java? |
---|
A. int[] numbers = {1, 2, 3};
B. int numbers[] = {1, 2, 3};
C. int numbers = {1, 2, 3};
D. Both A and B
View Answer Discuss Work SpaceAnswer: option d
Explanation:
22. | What is the output of the following code? public class Test { public static void main(String args[]) { String name = "Java"; System.out.println(name.substring(1, 3)); } } |
---|
A. Ja
B. av
C. Jav
D. No output
View Answer Discuss Work SpaceAnswer: option b
Explanation:
The substring() method returns a substring of the original string, starting at index 1 and ending at index 3 (exclusive). In this case, the output is "Ja".
23. | Which of the following is NOT a primitive data type in Java? |
---|
A. int
B. boolean
C. double
D. string
View Answer Discuss Work SpaceAnswer: option d
Explanation:
24. | What is the output of the following code? public class Test { public static void main(String args[]) { int x = 5; int y = 2; System.out.println(x / y); } } |
---|
A. 2
B. 2.5
C. 2
D. None of the above
View Answer Discuss Work SpaceAnswer: option a
Explanation:
When dividing two integers, the result is also an integer. In this case, 5 divided by 2 is 2 with a remainder of 1.
25. | What is the output of the following code? public class Test { public static void main(String args[]) { String name = "Java"; for(int i = 0; i < name.length(); i++) { System.out.print(name.charAt(i) + " "); } } } |
---|
A. J a v a
B. Java
C. Jv
D. No output
View Answer Discuss Work SpaceAnswer: option a
Explanation:
The code loops through each character in the string and prints it out with a space. In this case, the output is "J a v a".