Recognising Java keywords is not enough if you lose a variable's value inside a loop, confuse a class with an object, or cannot predict the exact output. A small result analyser over the five marks {72, 41, 88, 59, 35} settles what a program actually does: it prints total=295, average=59.0, highest=88, passed=4, and every one of those numbers is derived one iteration at a time. The same five marks then carry the class, string and collection ideas.
Java programming language: from source file to running class
Java source code for this example lives in ResultAnalyser.java. Running javac ResultAnalyser.java asks the JDK's compiler to translate that source into ResultAnalyser.class, which contains bytecode. Running java ResultAnalyser then asks the Java Virtual Machine (JVM) to load and execute the class.
The class file is the compiled form, while the JVM is the runtime that executes its bytecode. A public top-level class named ResultAnalyser must be in a file named ResultAnalyser.java.
public static void main(String[] args) is the program entry method. String[] args holds command-line text passed to it. Java is statically typed, class-based and case-sensitive, so a variable has a declared type and names such as marks and Marks are different.
The wider Coding & DSA Courses for Placements route helps you compare Java with other programming and data-structure options.

Java data types, variables and operators with exact values
A declaration gives a variable its type and name. An initialiser supplies its first value.
Declaration | Kind | Value |
|---|---|---|
| Primitive |
|
| Primitive |
|
| Primitive |
|
| Primitive |
|
| Reference |
|
| Reference | Five integers |
Reassignment, such as total = 300, changes the stored value without redeclaring the variable. If grade is declared inside an if block, it is in scope only within that block.
Types also control arithmetic. Integer division makes 7 / 2 equal 3, while 7 / 2.0 equals 3.5. Casting before division makes (double) 295 / 5 equal 59.0. Precedence makes 2 + 3 * 4 equal 14; parentheses make (2 + 3) * 4 equal 20.
Arithmetic operators calculate, comparison operators produce booleans, logical operators combine booleans, and assignment operators update variables. With average = 59.0 and passed = 4, both parts of average >= 50.0 && passed >= 4 are true, so the complete expression is true.
Java arrays and control flow: build the worked result analyser
Here is the complete program:
public class ResultAnalyser {
public static void main(String[] args) {
int[] marks = {72, 41, 88, 59, 35};
int total = 0;
int highest = marks[0];
int passed = 0;
for (int mark : marks) {
total += mark;
if (mark > highest) {
highest = mark;
}
if (mark >= 40) {
passed++;
}
}
double average = (double) total / marks.length;
System.out.printf(
"total=%d, average=%.1f, highest=%d, passed=%d%n",
total, average, highest, passed
);
}
}The enhanced for loop processes one mark at a time. These are the values after each iteration:
mark | total | highest | passed |
|---|---|---|---|
72 | 72 | 72 | 1 |
41 | 113 | 72 | 2 |
88 | 201 | 88 | 3 |
59 | 260 | 88 | 4 |
35 | 295 | 88 | 4 |
Thus average = (double) 295 / 5 = 59.0, and the exact output is:
total=295, average=59.0, highest=88, passed=4highest starts at marks[0] so the baseline is a real array value, not an arbitrary guess. The test mark >= 40 includes 40 itself. The cast comes before division so Java performs floating-point division instead of discarding any fractional part. If you need these language ideas sequenced from the beginning, use the Java Course, Concepts, MCQs and Coding Questions.

Java methods, classes and OOP on the same five marks
Move that same data and behaviour into a StudentResult class and the numbers arrive from methods rather than a loop inside main:
public class StudentResult {
private final String name;
private final int[] marks;
public StudentResult(String name, int[] marks) {
this.name = name;
this.marks = marks;
}
public String name() {
return name;
}
public double average() {
int total = 0;
for (int mark : marks) {
total += mark;
}
return (double) total / marks.length;
}
public boolean isEligible() {
return average() >= 50.0;
}
}In StudentResult asha = new StudentResult("Asha", new int[]{72, 41, 88, 59, 35}), asha is a reference variable and new StudentResult(...) creates the object. asha.average() returns 59.0 and asha.isEligible() returns true, the same two values the loop produced.
marks is private, so those methods are the only way to it, and that is encapsulation. Abstraction lets a caller use average() without repeating the loop.
Inheritance should express a genuine is-a relationship. Composition expresses a has-a relationship and is often the cleaner default.
Overloading and overriding are different. print(int total) and print(double average) are overloaded methods in one class because their parameter types differ. If a subclass supplies its own implementation of an inherited format() method, that is overriding, and runtime dispatch selects the implementation for the actual object.
Java strings, exceptions and collections complete the foundation
Consider String a = "Java"; String b = new String("Java");. In this construction, a == b is false because it compares references, while a.equals(b) is true because it compares content. Strings are immutable: after String label = "Java"; label.concat(" 21");, label remains "Java". Assigning the returned value produces "Java 21". The pool that decides which of those references are shared is worked through in String Handling in Java: String Pool, Immutability and Fresher-Test Questions.
Integer.parseInt("59") produces integer 59. Invalid text follows another control-flow path:
try {
Integer.parseInt("59x");
} catch (NumberFormatException error) {
System.out.println("Invalid mark: 59x");
}This handles a runtime exception. It does not repair a compile-time syntax or type error, which must be corrected before the program runs.
The five-element int[] marks has a fixed length. An ArrayList<Integer> can grow: after scores.add(72), scores.add(41) and scores.add(88), it is [72, 41, 88]. After scores.remove(Integer.valueOf(41)), it is [72, 88].
Java traps that change the result or stop the program
marks[5]is outside the array because its valid indices are0to4. Use an index within that range.295 / 5happens to produce the expected integer, but296 / 5produces59, not59.2. Cast first or make one operand floating point.a == bcompares the two references above. Usea.equals(b)to compare string content.if (total = 295)does not compile because the assignment expression has typeint, notboolean. Writeif (total == 295).Calling a method through
StudentResult result = nullthrowsNullPointerException. Initialiseresultor guard it explicitly before dereferencing it.
Java always passes arguments by value. For a primitive, changing the copied value does not change the caller's variable. For an object, the copied value is a reference, so a method can mutate the referenced object but cannot replace the caller's reference variable.
Java questions: trace values, explain OOP and write small programs
Useful practice includes predicting exact output, finding type or bounds errors, tracing arrays and loops, distinguishing overloading from overriding, following exception control flow, and choosing a collection. KnowledgeGate carries over 300 Java practice questions of exactly these kinds.
Try these five checks:
With
int x = 7; System.out.println(x++ + ++x);, Java evaluates left to right. The first operand contributes7, thenxbecomes8; the second increment makes it9and contributes9. It prints16and leavesxas9.With
String s = "Java"; s.concat(" 21"); System.out.println(s);, it printsJavabecause the returned string was not assigned.With
int[] a = {3, 1, 2}; Arrays.sort(a); System.out.println(a[0] + a[2]);, sorting produces{1, 2, 3}, so it prints4.Say why
asha.marksdoes not compile from another class, whileasha.average()does. The field is private, so a caller receives the computed59.0and never a handle on the array it came from. That is encapsulation earning its keep, not a slogan.Write
int countAbove(int[] marks, int threshold)returning how many marks reach the threshold. On{72, 41, 88, 59, 35}it must return4at threshold40and2at threshold60.
These tracing and problem-solving skills support broader CS preparation, semester study, placements and interviews. Once loops, arrays and methods are secure, Dynamic Programming Explained: 0/1 Knapsack is a sensible later step.
The short version and the next Java step
Keep this six-part chain: source becomes bytecode; types constrain valid operations; control flow changes state; arrays hold same-type values; methods package behaviour; classes bind state and behaviour. In the result analyser above, those ideas produced total 295, average 59.0, highest 88 and passed 4.
Now change one thing at a time. Raising the pass mark from 40 to 60 makes only 72 and 88 pass, so passed changes from 4 to 2; total, average and highest stay unchanged. Adding mark 65 instead makes total 295 + 65 = 360, length 6, average 360 / 6 = 60.0, highest 88, and the pass count at threshold 40 equal to 5.
For the next step, the DSA using Java, Placement Preparation Course applies this foundation to data structures and interview-oriented problems.




