Introduction To Java Programming Language

105606
Back to Introduction To Java Programming Language

Module 1: Introduction to Java

  1. 1(a). Which component makes Java bytecode executable on a particular operating system? (i) JVM (ii) Javadoc (iii) JDK (iv) Java compiler20262m

    Module 1: Introduction to Java

    Which component makes Java bytecode executable on a particular operating system?

    (i) JVM
    (ii) Javadoc
    (iii) JDK
    (iv) Java compiler

    View this question on its own page →
    Worked Solution

    Answer: (i) JVM

    The Java Virtual Machine (JVM) executes Java bytecode on the host operating system. The compiler converts .java source into platform-independent .class bytecode; a compatible JVM interprets or JIT-compiles it for that platform.

    MermaidDiagram
    Rendering diagram…
  2. 1(c). Which statement terminates the current iteration of a loop and proceeds with the next iteration? (i) break (ii) return (iii) continue (iv) exit20262m

    Module 1: Introduction to Java

    Which statement terminates the current iteration of a loop and proceeds with the next iteration?

    (i) break
    (ii) return
    (iii) continue
    (iv) exit

    View this question on its own page →
    Worked Solution

    Answer: (iii) continue

    continue skips the remaining statements in the current loop iteration and moves to the next iteration. break exits the loop; return exits the current method.

    for (int i = 1; i <= 5; i++) {
        if (i == 3) continue;
        System.out.print(i + " "); // 1 2 4 5
    }
    
  3. 2(a). Compare Java and C++ with respect to memory management, pointers, inheritance, platform independence, exception handling and object-oriented features. Identify the design choices that make Java different from C++.20267m

    Module 1: Introduction to Java

    Compare Java and C++ with respect to memory management, pointers, inheritance, platform independence, exception handling and object-oriented features. Identify the design choices that make Java different from C++.

    View this question on its own page →
    Worked Solution

    Java vs C++ (7-mark answer)

    Aspect Java C++
    Memory Automatic garbage collection Manual management and RAII/smart pointers
    Pointers No general pointer arithmetic exposed Supports pointers and pointer arithmetic
    Inheritance A class extends one class; multiple interfaces allowed Multiple class inheritance supported
    Portability Bytecode runs on compatible JVMs Usually compiled to native platform code
    Exceptions Checked and unchecked exceptions Primarily unchecked exception model
    Operator overloading Generally not supported for user classes Supported
    Generic types Generics, mostly type-erased Templates

    Java emphasizes managed memory, safety and JVM portability; C++ offers more low-level control and often direct native performance. Both support object-oriented programming, encapsulation, inheritance and polymorphism.

    MermaidDiagram
    Rendering diagram…
  4. 2(b). A Java program is required to run on different operating systems without recompilation of the source code. Analyse the role of bytecode, JVM and JDK in achieving Java's platform-independent execution model.20267m

    Module 1: Introduction to Java

    A Java program is required to run on different operating systems without recompilation of the source code. Analyse the role of bytecode, JVM and JDK in achieving Java's platform-independent execution model.

    View this question on its own page →
    Worked Solution

    Bytecode, JVM and JDK

    1. Source: The programmer writes .java files.
    2. Compiler: javac (part of the JDK) compiles source into bytecode in .class files.
    3. JVM: A platform-specific JVM loads, verifies and executes bytecode, interpreting it or compiling hot code with a JIT compiler.
    4. JDK: The Java Development Kit contains development tools such as javac, runtime components and debugging/documentation tools. The JVM is the execution engine, not the compiler.
    MermaidDiagram
    Rendering diagram…

    Thus Java source generally need not be recompiled for each OS, provided a compatible JVM and required libraries are available.

  5. 3(a). Analyse Java's primitive data types and type conversion mechanisms. Construct suitable examples showing implicit widening and explicit narrowing conversion, including one case where data loss occurs.20267m

    Module 1: Introduction to Java

    Analyse Java's primitive data types and type conversion mechanisms. Construct suitable examples showing implicit widening and explicit narrowing conversion, including one case where data loss occurs.

    View this question on its own page →
    Worked Solution

    Primitive types and conversion

    Java has eight primitive types: byte, short, int, long, float, double, char, and boolean. Numeric types differ in range and precision; boolean represents true/false and is not numeric.

    Widening converts a type to a type with a broader range, usually implicitly:

    int count = 25;
    double value = count; // 25.0, no narrowing loss
    

    Narrowing converts to a smaller or less precise type and requires a cast:

    double price = 19.95;
    int whole = (int) price; // 19: fractional part lost
    int n = 130;
    byte b = (byte) n; // wraps to -126 for 8-bit signed byte
    

    A cast permits the conversion; it does not guarantee that the original value is preserved. Use appropriate types and validate ranges.

  6. 3(b). Construct a Java program that prints a triangular pattern of numbers and analyse how the for loop controls the number of rows and columns.20267m

    Module 1: Introduction to Java

    Construct a Java program that prints a triangular pattern of numbers and analyse how the for loop controls the number of rows and columns.

    View this question on its own page →
    Worked Solution

    Number triangle using nested for loops

    public class Triangle {
        public static void main(String[] args) {
            int rows = 5;
            for (int i = 1; i <= rows; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.print(j + " ");
                }
                System.out.println();
            }
        }
    }
    

    Output:

    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
    

    The outer loop controls rows (i = 1 to rows). The inner loop prints i numbers on row i; println() moves to the next line. There are (1+2+\cdots+n=n(n+1)/2) printed values, so time complexity is O(n²) and auxiliary space is O(1).

  7. 4(a). Consider the following code segment: ``java int a = 5; int b = 2; System.out.println(a++ + ++b * a); `` Determine the value printed by the program and show the evaluation sequence according to Java's operator and increment rules.20267m

    Module 1: Introduction to Java

    Consider the following code segment:

    int a = 5;
    int b = 2;
    System.out.println(a++ + ++b * a);
    

    Determine the value printed by the program and show the evaluation sequence according to Java's operator and increment rules.

    View this question on its own page →
    Worked Solution

    Evaluate the expression

    int a = 5;
    int b = 2;
    System.out.println(a++ + ++b * a);
    

    Java evaluates operands left-to-right; multiplication has higher precedence than addition.

    1. a++ yields the old value 5, then a becomes 6.
    2. ++b increments b from 2 to 3, and yields 3.
    3. The later a is now 6, so 3 * 6 = 18.
    4. Addition gives 5 + 18 = 23.

    Output: 23. Final values are a = 6, b = 3.