Knowee
Questions
Features
Study Tools

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

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:

  1. The program starts with creating a string "abcdefgh" and getting its length which is 8.

  2. 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.

  3. 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.

  4. 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.

  5. 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'.

  6. Therefore, the output of the program is "abcd".

This problem has been solved

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?

1/3

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.