Read the if program segment given below:
if (a > b)
z = 25;
else
z = 35;Which one of the following is the correct conversion of the if segment to ternary?
Step 1: Understand the given if-else statement
The logic states:
If $(a > b)$, then $z = 25$.
Otherwise, $z = 35$.
The value assigned to $z$ depends strictly on the boolean result of the condition $(a > b)$.
Step 2: Recall the syntax of the Ternary Operator
The ternary (conditional) operator follows this structure:variable = (condition) ? value_if_true : value_if_false;
Step 3: Apply the logic to the given statement
Plugging these into the syntax:z = (a > b) ? 25 : 35;
Step 4: Comparison of Options
Final Answer:
$z = a > b ? 25 : 35;$
Rewrite the following program segment using a for loop:
Given program segment:
int a = 5, b = 10;
while (b > 0)
{
b -= 2;
}
System.out.println(a * b);
Note: Ensure that the variable $b$ remains accessible for the System.out.println statement after the loop terminates.
Give the output of the following program segment and mention how many times the loop is executed.
int K = 1;
do
{
K += 2;
System.out.println(K);
} while (K <= 6);
ब्रेक स्टेटमेंट को __________ स्टेटमेंट में केस खत्म करने के लिए प्रयोग किया जाता है।
Give the output of the following Java program segment:
String a[] = {"Atasi", "Aditi", "Anant", "Amit", "Ahana"};
System.out.println(a[1].charAt(1) + "*" + a[2].charAt(2));
The output of the above statement is:
Assertion (A): The substring() method modifies the original String.
Reason (R): The substring() method can extract part of a String from a specific index.
Define a class named StepTracker with the following specifications:
Member Variables:
String name — stores the user's name.int sw — stores the total number of steps walked ($sw$).double cb — stores the estimated calories burned ($cb$).double km — stores the estimated distance walked in kilometers ($km$).Member Methods:
1. void accept()
To input the name and the sw (steps walked) using Scanner class methods only.
2. void calculate()
Calculates calories burned ($cb$) and distance in ($km$) based on steps walked ($sw$) using the provided estimation logic.