Solution to Mid Term
Solution to Mid Term
It executes a block of code only if a specified condition is true, enabling branching and control
flow.
o "Method overloading" (or function overloading) allows you to define multiple methods with the
same name but different parameter lists (different number, types, or order of parameters). The
compiler or interpreter determines which method to call based on the arguments provided in the
method call.
Dry Run:
o left is 0.
o right is 3 (4 - 1).
3. while loop:
o Iteration 1:
▪ left (0) < right (3) is true.
▪ processedStr.charAt(0) ('1') is compared to processedStr.charAt(3) ('1'). They are
equal.
▪ left becomes 1.
▪ right becomes 2.
o Iteration 2:
▪ left (1) < right (2) is true.
▪ processedStr.charAt(1) ('0') is compared to processedStr.charAt(2) ('0'). They are
equal.
▪ left becomes 2.
▪ right becomes 1.
o The loop condition left (2) < right (1) is now false, and the loop terminates.
4. checkString method returns true.
5. Back in the main method:
o result is true.
o "Input '1001' meets the required property: True" is printed to the console.
o The Scanner is closed.
Output:
1. a > b:
o 15 > 12 is true.
2. (a > c) ? a : c:
o Since a > b is true, we evaluate this part.
o a > c is 15 > 20, which is false.
o Therefore, the result of this inner ternary operator is c, which is 20.
3. largest = 20:
o The variable largest is assigned the value 20.
Therefore, the output of the code is that the variable largest will hold the value 20.