Topics Index

Append values to StringBuffer2902

If you need explanation Read this topic

If you need Answer Take test on this topic

Append values to StringBuffer2902
What will be the output of the following program?
import java.util.Random;
public class Rhymes {
    private static Random rnd = new Random();
    public static void main(String[] args) {
        StringBuffer word = null;
        switch (rnd.nextInt(2)) {
            case 1 :
                word = new StringBuffer("P");
            case 2 :
                word = new StringBuffer("G");
            default :
                word = new StringBuffer("M");

A. Pain
B. Gain
C. Main
D. Some other output
E. Output can not determined
F. Compilation Error or Runtime Error
Topic: Java Random Class - java.util.Random Package

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.

d.. some other output..  It does not print Pain, Gain or Main ! It always prints ain .....A few explanations........  :a) The only possible values of the expression rnd.nextInt(2) are 0 and 1.   .....b) remember “break” in “switch” element !c) new StringBuffer(‘M’) ==> it’s actually the newStringBuffer(int) constructor which is used !.......the random number is chosen so the switch statement can reach only two of its three cases. The specification for Random.nextInt(int)says: "Returns a pseudorandom, uniformly distributed int value between0(inclusive) and the specified value (exclusive) This means that the only possible values of the expression rnd.nextInt(2)are 0 and1. The switch statement will never branch to case2, which suggests that the program will never print Gain. The parameter to nextInt should have been3rather than2.............there are no break statements between the cases. Whatever the value of the switch expression, the program will execute that case and all subsequent cases. Each case assigns a value to the variable word, and the last assignment wins. The last assignment will always be the one in the final case (default), which isnew StringBuffer('M'). This suggests that the program will never print Pain or Gainbut always Main..............There is a parameterless constructor, one that takes aString indicating the initial contents of the string buffer and one that takes an intindicating its initial capacity. In this case, the compiler selects theintconstructor, applying awidening primitive conversionto convert thechar value'M'into the int value77.In other words,new StringBuffer('M')returns an emptystring buffer with an initial capacity of 77. The remainder of the program appends the characters a,i, and into the empty string buffer and prints out its contents, which are always ain..

Posted by Asma Mujtaba Khan    2014-12-11 08:56:44

ans c

java.util.Random class to generate random values. It's a pseudo-random number
generator that can be initialized with a 48-bit seed.

Random. This class
provides pseudorandom numbers. It contains several methods that allow you to obtain
random numbers in the form required by your program.

The nextInt(int n) method is used to get a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

A number of methods operate on the String and return a new String as a result. While this is useful, you should
be aware that creating lots of strings in this manner can affect performance. If you need to modify a string often, you
should use the StringBuffer class.

StringBuffer is a peer class of String that provides much of the functionality of strings.
As you know, String represents fixed-length, immutable character sequences. In contrast,
StringBuffer represents growable and writeable character sequences. StringBuffer may have
characters and substrings inserted in the middle or appended to the end. StringBuffer will
automatically grow to make room for such additions and often has more characters preallocated
than are actually needed, to allow room for growth.

coming to this program here initially random object .
stringbuffer word is assigned with null object .

rnd.nextInt returns 0 and 1 randomly ,in switch case no break statement is used so always word assigned with "M"

aft this word is apped with a,i,n chars .

so sop prints Main

Posted by Maheshwari Natarajan    2014-12-11 18:18:00


Posted by Shubham Bansal    2014-12-13 09:23:01

This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Sai Ram,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Maheshwari Natarajan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-12-14 01:27:23

© meritcampus 2019

All Rights Reserved.

Open In App