Topics Index

Java Comes

If you need explanation Read this topic

If you need Answer Take test on this topic

Java Comes
What will be the output of the following program?
public class JavaComes {
    public static void main(String[] args) {
        String input = "Will java comes?";
        String input1 = input.substring(0, 3);
        input = input.replace('i', 'e');
        input = input.replace("?", "");
        input1.replace('i', 'e');
        input1 = input + "\n" + input1.replace('i', 'e') + input.substring(10, 14) + " to " + input.substring(5, 10) + "School";
        System.out.println(input = input1);

A. Will java comes? 
Welcome to java School
B. Well java comes 
Welcome to java School
C. Will java comes?
Well java comes
Welcome to java School
D. Will java comes? 
Will java comes?
E. Some other output
F. Compilation Error or Runtime Error
Topic: replace() Method In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans is A...1st tim it wl print will java comes n 2nd tim welcome to java school..i get reaplced wit e n input1 contains wil n latr it wl replaced

Posted by Uday Kumar    2014-05-09 13:06:55

Ans is E..Initially,input is printed as Will java comes? and then after replacements and concatenations the output is Well java comes\nWellcome to java School

Posted by Badam Swathi    2014-05-09 15:39:49

First SOP prints out the value of "input" String variable. Then a sub string of input is obtained from 0 to 2 and assigned to "input1" i.e., "Wil". input will be "Well java comes" after the subsequent calls to replace(). Then the next call to replace() on input1 replaces all the occurrences of 'i' with 'e' and since we are not collecting the result ("Wel" here) in a variable, it will be ignored (or the result should be used immediately to see the change effect) (That means, replace() doesn't modify the original string --> String objects are immutable.) As a result of all replacement and concatenation operations, input1 will have the value
"Well java comes\nWelcome to java School". So, the answer is C.

Posted by Shaileshwar Sharma    2014-05-09 16:16:35

No winner for this dose. Correct option is 'C' :(. Shaileshwar Sharma was already won in this week.

Posted by Merit Campus    2014-05-12 04:21:17

© meritcampus 2019

All Rights Reserved.

Open In App