Knowee
Questions
Features
Study Tools

What is the output of the following code?public class Main {  public static void main(String[] args) {    String str1 = "NPTEL";    String str2 = "java";    int a = 20;    int b = 24;    System.out.println(str1 + a + b); // Statement 1     System.out.println(a + b + str2); // Statement 2  }} a. NPTEL2024         44java b. NPTEL44         44java c. NPTEL2024         2024java d. NPTEL44         2024java

Question

What is the output of the following code?public class Main {  public static void main(String[] args) {    String str1 = "NPTEL";    String str2 = "java";    int a = 20;    int b = 24;    System.out.println(str1 + a + b); // Statement 1     System.out.println(a + b + str2); // Statement 2  }} a. NPTEL2024         44java b. NPTEL44         44java c. NPTEL2024         2024java d. NPTEL44         2024java

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code will be:

a. NPTEL2024 44java

Explanation:

In Java, the '+' operator is used for both addition and string concatenation. The operation is performed from left to right.

For Statement 1: The first operation is a string (str1) concatenated with an integer (a), which results in a string. Then, another integer (b) is concatenated, resulting in "NPTEL2024".

For Statement 2: The first operation is an addition of two integers (a and b), which results in an integer. Then, a string (str2) is concatenated, resulting in "44java".

This problem has been solved

Similar Questions

What is the output of the following code snippet? String str1 = "Java"; String str2 = new String("Java"); System.out.println(str1 == str2);

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 will be the output of the following program?class string_class {public static void main(String args[]){String obj = "hello";String obj1 = "world";   String obj2 = obj;obj2 = " world";System.out.println(obj + " " + obj2); }}

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 is the output of the following Java code?Code:class HackerEarth{ public void print() { System.out.println("Inside HackerEarth"); }}class Hacker extends HackerEarth{ public void print() { System.out.println("Inside Hacker"); }}public class HackerEarthMain{ public static void main(String[] args) { HackerEarth obj1 = new HackerEarth(); Hacker obj2 = new Hacker(); ((HackerEarth) obj2).print(); obj1 = obj2; obj1.print(); }}

1/3

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.