Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

What will be the output of the following program?
public class PrintValues
    public static void main(String args[])
        System.out.printf("%d %d", 10, 20, 30);

A. 30 20
B. 10 20
C. 20 30
D. 10 20 30
E. Compilation Error or Runtime Error
Topic: Java Formatter

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.

here option B
excution starts from main here a statement System.out.printf("%d %d",10,20,30);
System.out.printf() can be used to send formatted numerical output to the console.
It uses a java.util.Formatter object internally and so emulates the printf() function in C.
as we know about printf in c the same functionality will be done here
here System.out.println("%d %d",10,20,30); consist a string in that two format specifiers are there type was integer
but along with the string another 3 integer values are there 10,20,30 all these  arguments are passed to printf() method
that method scan the string and first its consist "%d %d "means it wants two integer values but there three values at this time it will take first two values(10,20) and ignore the rest

thats values it prints 10,20 option B

Posted by Goutham Singarapu    2015-03-02 12:18:38

Ans is B  program execution starts with main() here system.out.printf("%d %d" ,10,20,30) here in printf()is used to print  here in printf function there are two format string  of type int so it prints two numbers 10 20

Posted by Ramya Reddy Addulla    2015-03-02 12:26:45

option E compilation error or runtime error beacuse theres no printf function in java till now :)

Posted by Harish Kumar Thumu    2015-03-02 12:45:19

ANS IS B ( 10 20 )

--Here, Printf() function is used to print the output on the screen.

--printf() function is also there in java,which supports the format specifier along with the data to be printed.

--Here, "%d" format specifier is used which is used for integer numbers to be printed on the console.

--Two format specifiers are used that means system expects two integers.
But here 3 integers are used, so first two integers will be printed and others will be ignored.

10 20

Posted by Mânïshå Mùlchåndânï    2015-03-02 16:47:47

ans b...system. out.printf() is a new way to format output that is similar to C's printf . Each argument in printf() contains format strings and objects to be output using the specified format. The general form for printf() will be the following: System.out.printf (String format, Object... args); The format string contains all the formatting instructions to format the variable number of Object arguments. Object... indicates that a variable number of arguments are permitted. You can replace System.out.printf() with System.out.format() if you prefer. There is no difference between System.out.format() and System.out.printf () . The first parameter, format , is a format string specifying how the objects in the second parameter, args , are to be formatted. The format string contains plain text as well as format specifiers, which are special characters that format the arguments of Object... args . (The notation Object... args is called varargs, which means that the number of arguments may vary.) Format specifiers begin with a percent sign (%) and end with a converter . The converter is a character indicating the type of argument to be formatted. In between the percent sign (%) and the converter you can have optional flags and specifiers.coming to this program, system. out.printf() has 2 format specifiers, 3 args,so only 1st 2 arg of int values get printed.compiler ignores 3 rd arg which dnt have format specifier. so 10 and 20 get printed. 30 ignored

Posted by Maheshwari Natarajan    2015-03-02 18:07:22

This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Switguy Pawan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-03-03 03:44:07

© meritcampus 2019

All Rights Reserved.

Open In App