What will be the output of the below code? public static void main(String args[]) { int rollNo = 101; float newRollNo = rollNo; System.out.println(newRollNo); }
Question
What will be the output of the below code? public static void main(String args[]) { int rollNo = 101; float newRollNo = rollNo; System.out.println(newRollNo); }
Solution
The output of the code will be 101.0.
Here's the step by step explanation:
-
The
mainmethod is the entry point of the program. -
An integer variable
rollNois declared and initialized with the value 101. -
A float variable
newRollNois declared and it's initialized with the value ofrollNo. SincerollNois an integer, it's automatically converted to a float. This is called type casting. -
The
System.out.println(newRollNo);statement prints the value ofnewRollNoto the console. SincenewRollNois a float, it's printed with a decimal point. So, 101 becomes 101.0.
Similar Questions
e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}
What is the output of the following code snippet?ArrayList<Integer> numbers = new ArrayList<>();numbers.add(1);numbers.add(2);numbers.add(3);System.out.println(numbers.get(2));
What will be the output of the following Java program?class java { public static void main(String args[]) { int var1 = 3; int var2 = 8; if ((var2 = 2) == var1) System.out.print(var2); else System.out.print(++var2); } }Options3421
What will be the output of the following code ?public static void print(int n){ if(n < 0){ return; } if(n == 0){ System.out.println(n); return; } print(n--); System.out.print(n+" ");}public static void main(String[] args) { int num = 3; print(num);
Select the correct answerWhat will be the output of the following Java program?class Output { public static void main(String args[]) { double ct1 = 3.0; double ct2 = 2.0; double ct3 = Math.pow( ct1, ct2 ); System.out.print(ct3); } }Options3.02.09.08.0
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.