Introduction To Java Programming Language
105606Module 1: Introduction to Java
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
View this question on its own page →Which component makes Java bytecode executable on a particular operating system?
(i) JVM
(ii) Javadoc
(iii) JDK
(iv) Java compilerWorked SolutionAnswer: (i) JVM
The Java Virtual Machine (JVM) executes Java bytecode on the host operating system. The compiler converts
.javasource into platform-independent.classbytecode; a compatible JVM interprets or JIT-compiles it for that platform.MermaidDiagramRendering diagram…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
View this question on its own page →Which statement terminates the current iteration of a loop and proceeds with the next iteration?
(i) break
(ii) return
(iii) continue
(iv) exitWorked SolutionAnswer: (iii)
continuecontinueskips the remaining statements in the current loop iteration and moves to the next iteration.breakexits the loop;returnexits the current method.for (int i = 1; i <= 5; i++) { if (i == 3) continue; System.out.print(i + " "); // 1 2 4 5 }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
View this question on its own page →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++.
Worked SolutionJava 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.
MermaidDiagramRendering diagram…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
View this question on its own page →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.
Worked SolutionBytecode, JVM and JDK
- Source: The programmer writes
.javafiles. - Compiler:
javac(part of the JDK) compiles source into bytecode in.classfiles. - JVM: A platform-specific JVM loads, verifies and executes bytecode, interpreting it or compiling hot code with a JIT compiler.
- 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.
MermaidDiagramRendering diagram…Thus Java source generally need not be recompiled for each OS, provided a compatible JVM and required libraries are available.
- Source: The programmer writes
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
View this question on its own page →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.
Worked SolutionPrimitive types and conversion
Java has eight primitive types:
byte,short,int,long,float,double,char, andboolean. Numeric types differ in range and precision;booleanrepresents 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 lossNarrowing 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 byteA cast permits the conversion; it does not guarantee that the original value is preserved. Use appropriate types and validate ranges.
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
View this question on its own page →Construct a Java program that prints a triangular pattern of numbers and analyse how the for loop controls the number of rows and columns.
Worked SolutionNumber triangle using nested
forloopspublic 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 5The outer loop controls rows (
i = 1torows). The inner loop printsinumbers on rowi;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).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
View this question on its own page →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.
Worked SolutionEvaluate 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.
a++yields the old value 5, thenabecomes 6.++bincrementsbfrom 2 to 3, and yields 3.- The later
ais now 6, so3 * 6 = 18. - Addition gives
5 + 18 = 23.
Output:
23. Final values area = 6,b = 3.