>
Exams
>
Computer Science
>
Java Operators
>
what will be the output of the following java code
Question:
What will be the output of the following Java code?
public class Test {
public static void main(String[] args) {
int x = 5;
System.out.println(++x * 2);
}
}
Show Hint
Pre-increment (++x) updates the variable first, then that new value is multiplied.
CPET - 2025
CPET
Updated On:
Jul 2, 2026
12
10
8
14
Show Solution
Verified By Collegedunia
The Correct Option is
A
Solution and Explanation
Step 1:
Start with \( x = 5 \).
Step 2:
The operator
++x
is a pre-increment. It raises x by 1 before the value is used, so x becomes 6 and the expression uses 6.
Step 3:
Now multiply. \( 6 \times 2 = 12 \).
Step 4:
println prints 12. Answer is (A).
Download Solution in PDF
Was this answer helpful?
0
0
Top CPET Computer Science Questions
Which of the following is a key reason for choosing DRAM over SRAM for main memory?
CPET - 2025
Computer Science
Computer Organization - Memory Technology
View Solution
Which of the following components in an FPGA is responsible for implementing combinational and sequential logic?
CPET - 2025
Computer Science
Digital Design - FPGA Architecture
View Solution
What is the main purpose of declaring a base class as virtual in multiple inheritance?
CPET - 2025
Computer Science
OOP in C++ - Virtual Inheritance
View Solution
What happens if a class contains at least one pure virtual function?
CPET - 2025
Computer Science
OOP in C++ - Abstract Classes
View Solution
What happens if a derived class defines a function with the same name as a base class function, but with a different parameter list?
CPET - 2025
Computer Science
C++ Inheritance and Name Hiding
View Solution
View More Questions
Top CPET Java Operators Questions
What is the use of the final keyword in Java?
CPET - 2025
Computer Science
Java Operators
View Solution
What will be the output of the following Java code?
public class Check {
public static void main(String[] args) {
int a = 5;
System.out.println(a > 2 ? a < 5 ? 10 : 20 : 30);
}
}
CPET - 2025
Computer Science
Java Operators
View Solution
Top CPET Questions
Which of the following is a key reason for choosing DRAM over SRAM for main memory?
CPET - 2025
Computer Organization - Memory Technology
View Solution
Which of the following components in an FPGA is responsible for implementing combinational and sequential logic?
CPET - 2025
Digital Design - FPGA Architecture
View Solution
What is the main purpose of declaring a base class as virtual in multiple inheritance?
CPET - 2025
OOP in C++ - Virtual Inheritance
View Solution
What happens if a class contains at least one pure virtual function?
CPET - 2025
OOP in C++ - Abstract Classes
View Solution
What happens if a derived class defines a function with the same name as a base class function, but with a different parameter list?
CPET - 2025
C++ Inheritance and Name Hiding
View Solution
View More Questions