Topics Index

Javalang Package

If you need explanation Read this topic

If you need Answer Take test on this topic

Javalang Package
What will be the output of the following program?
import static java.lang.System.*;
public class BeautifulFlowers {
    public static void main(String[] args) {
        System.out.println("Printed 1");
        out.print("Printed 2 ");
        int happyDays[] = {1, 2, 3, 4};
        for (int inBtech : happyDays)
            out.print(inBtech < happyDays[3] ? inBtech : ' ');
        out.append(' ');

A. Printed 1
Printed 2 Happy
12332 yearsCompleted
B. Printed 1
Printed 2 
123  years
C. Printed 1
Printed 2 Completed
D. Some other output
E. Output can not be determined
F. Compilation Error or Runtime Error
Topic: Unknown

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.
because out. append() function will join the string one by one.
In for loop conditions will be checked and all becomes true and print 12332

after that append function will do their work and ans A will be printed....

Posted by Shubham Bansal    2014-10-21 08:54:48


In this program
----Static System Class is imported so we need not to write classname.methodname i.e. System.out.println for displaying the output we can simply use out.print() or out.println().

--System.out.println("printed 1") will print "printed 1" and cursor will go to next line.

--out.print("printed 2 ") -> prints printed 2

--out.append("happy\n") -> appends Happy to outputstream i.e. prints Happy on screen  and cursor will go to next line.

--for loop -> inBtech is assigned with every value of happyDays array's element and ternary operator is used as below :

if(1<happyDays[3] i.e. 4 ?inBtech : ' ')
1<4 so 1 is printed
2<4 so 2 is printed
3<4 so 3 is printed
4<4 ->false so ' ' should get printed but ascii value for space is "32" which gets prited.

--out.append(' ') ->appends character to outputstream and print them on screen.
so years get printed on screen.

--out.print("completed") ->prints completed on screen.

Posted by Mânïshå Mùlchåndânï    2014-10-21 09:59:56

ns is A..Here we are using the concept of the static import which cam java 1.5 version..
use of static import is when you import an class or package as static all static varibles and methods wil come...we no need to cal them by using the Class Name..directly we can access them But problem wih this we cant use varible names as there in inbuilt classes...

coming to example here we import System class static so that all static methods and variables we can access directly without using the class name

Before Going into example brieff intruction about Sop()

out is an reference of class PrintStream, out is a static variable in System class print(),println(),append() all are methods present in the class we import System as static we dont need to use System class name to access the out variable
print()--> print the content,cursor wil point next position

println()--> prints the content and cursor wil move next line

append()--> will append the sequence of chars to output stream

in 1st sop() printed1 wil print cursor wl move next line
2nd sop() Printed2 wil print cursor wil point next location there we are adding Happy ad manually we are moving to next line using \n

created an array with 4 elements
travelling using the for each loop which wil travel through out the array

here condition wil true for 1st three iterations so 123 get printed last time condtion fails so ' ' printed as its ascii value 32

next we are adding char by char as 'y','e','a','r','s' lastlt we are prnting completed

Posted by Uday Kumar    2014-10-21 15:41:57

This dose is now closed and the winners are Shubham Bansal,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Varun Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-10-22 03:43:40

© meritcampus 2019

All Rights Reserved.

Open In App