What will be the output of the following Java program? import java.io.*; class Chararrayinput { public static void main(String[] args) { String obj = "abcdefgh"; int length = obj.length(); char c[] = new char[length]; obj.getChars(0, length, c, 0); CharArrayReader input1 = new CharArrayReader(c); CharArrayReader input2 = new CharArrayReader(c, 1, 4); int i; int j; try { while((i = input1.read()) == (j = input2.read())) { System.out.print((char)i); } } catch (IOException e) { e.printStackTrace(); } } }A) abcB) abcdC) abcdeD) none of the mentioned
Question
What will be the output of the following Java program? import java.io.*; class Chararrayinput { public static void main(String[] args) { String obj = "abcdefgh"; int length = obj.length(); char c[] = new char[length]; obj.getChars(0, length, c, 0); CharArrayReader input1 = new CharArrayReader(c); CharArrayReader input2 = new CharArrayReader(c, 1, 4); int i; int j; try { while((i = input1.read()) == (j = input2.read())) { System.out.print((char)i); } } catch (IOException e) { e.printStackTrace(); } } }A) abcB) abcdC) abcdeD) none of the mentioned
Solution
The output of the given Java program will be "abcd". So, the correct option is B) abcd.
Here's the step by step explanation:
-
The program starts with creating a string "abcdefgh" and getting its length which is 8.
-
Then it creates a character array 'c' of the same length and copies the characters from the string into this array using the getChars() method.
-
Two CharArrayReader objects are created, input1 reads the whole array while input2 starts from the second character (index 1) and reads the next 4 characters.
-
Then it enters a while loop where it reads one character at a time from both input1 and input2 and compares them. If they are equal, it prints the character.
-
The loop continues until the characters from input1 and input2 are not equal. Since input2 only reads 4 characters starting from the second one, the loop will break after printing the fourth character of the string, which is 'd'.
-
Therefore, the output of the program is "abcd".
Similar Questions
What will be the output of the following Java program? class array_output { public static void main(String args[]) { char array_variable [] = new char[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = 'C'; System.out.print(array_variable[i] + "" ); i++; } } }OptionsCCCCCCCCCCCCCCCC D E F GNone of the mentioned
What will be the output of the following Java program? class mainclass { public static void main(String args[]) { char a = 'Z'; a++; System.out.print((int)a); } }Options9091122123
What will be the output of the below program?public class Test { public static void main(String[] args) { char c = 65; System.out.println("c = " + c); }} Note : explain clearly in the answer
What will be the output of the following Java program? package pkg; class output {public static void main(String args[]) { StringBuffer s1 = new StringBuffer("Hello"); s1.setCharAt(1, x); System.out.println(s1); } }
What is the output of the following Java program?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.