ect the correct answerWhat 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, j; try { while ((i = input1.read()) == (j = input2.read())) { System.out.print((char) i); } } catch (IOException e) { e.printStackTrace(); } }}
Question
ect the correct answerWhat 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, j; try { while ((i = input1.read()) == (j = input2.read())) { System.out.print((char) i); } } catch (IOException e) { e.printStackTrace(); } }}
Solution
The output of the Java program will be "bcde".
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 fills it with characters from the string using the getChars() method.
-
Two CharArrayReader objects are created - 'input1' reads from the entire array, while 'input2' reads only from the second to the fifth character (Java uses zero-based indexing).
-
Then the program enters a loop where it reads one character at a time from both 'input1' and 'input2' and compares them. If they are the same, it prints the character.
-
The loop continues until the characters read from 'input1' and 'input2' are different. Since 'input2' starts reading from the second character of the array, the first character where 'input1' and 'input2' will differ is the fifth character (index 4).
-
Therefore, the program will print the first four characters of the array, which are "bcde".
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
Select the correct answerWhat will be the output of the following Java program?class String_demo { public static void main(String args[]) { char chars[] = {'f', 'o', 'r'}; String s = new String(chars); System.out.println(s); }}Optionsfrforo
the correct answerWhat 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); } }Options9112290123
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
ct the correct answerWhat will be the output of the following Java code? class codetantra { public static void main(String args[]) { char a = 'A'; char b = 76; b++; a++; System.out.println(a + " " + b); } }
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.