Introduction To Java Programming Language

105606
Back to Introduction To Java Programming Language

Module 2: Arrays and Strings

  1. 1(b). A constructor is automatically invoked when: (i) A method is called (ii) A package is imported (iii) An object is created (iv) A class is inherited20262m

    Module 2: Arrays and Strings

    A constructor is automatically invoked when:

    (i) A method is called
    (ii) A package is imported
    (iii) An object is created
    (iv) A class is inherited

    View this question on its own page →
    Worked Solution

    Answer: (iii) An object is created

    A constructor runs when an object is instantiated with new (or through another constructor). It initializes the new object's state. Constructors have the class name and no return type.

    class Student {
        Student() { System.out.println("Created"); }
    }
    Student s = new Student(); // invokes constructor
    
  2. 4(b). A student record system stores marks of 50 students in an array. Construct a Java program to calculate the highest, lowest and average marks and analyse the time complexity of the operations.20267m

    Module 2: Arrays and Strings

    A student record system stores marks of 50 students in an array. Construct a Java program to calculate the highest, lowest and average marks and analyse the time complexity of the operations.

    View this question on its own page →
    Worked Solution

    Find highest, lowest and average marks

    public class Marks {
        public static void main(String[] args) {
            int[] marks = {72, 85, 61, 90, 77}; // example values
            int max = marks[0], min = marks[0], sum = 0;
            for (int mark : marks) {
                if (mark > max) max = mark;
                if (mark < min) min = mark;
                sum += mark;
            }
            double average = (double) sum / marks.length;
            System.out.println("Highest = " + max);
            System.out.println("Lowest = " + min);
            System.out.println("Average = " + average);
        }
    }
    

    Replace the example array with the 50 students' marks. Initializing max and min from the first element avoids assuming marks are non-negative. A single pass calculates all three values: O(n) time and O(1) extra space.

  3. 5(a). Consider the following code segment: ``java String s1 = "Java"; String s2 = new String("Java"); ` Analyse the results of s1 == s2 and s1.equals(s2)`.20267m

    Module 2: Arrays and Strings

    Consider the following code segment:

    String s1 = "Java";
    String s2 = new String("Java");
    

    Analyse the results of s1 == s2 and s1.equals(s2).

    View this question on its own page →
    Worked Solution

    == versus .equals()

    String s1 = "Java";
    String s2 = new String("Java");
    
    • s1 == s2 is false because == compares object references, and new String(...) creates a distinct object.
    • s1.equals(s2) is true because String.equals() compares character contents.

    For strings, use .equals() when comparing text. == is appropriate when reference identity is what you intend to test.

  4. 9(b). Write a short note on Constructors and Object Creation.20267m

    Module 2: Arrays and Strings

    Write a short note on Constructors and Object Creation.

    View this question on its own page →
    Worked Solution

    Constructors and object creation

    A constructor initializes an object's state. It has the same name as its class and no return type. Constructors may be no-argument, parameterized, or overloaded; if no constructor is declared, Java supplies a default no-argument constructor.

    class Book {
        String title;
        Book(String title) { this.title = title; }
    }
    Book b = new Book("Networks");
    

    new allocates an object, initializes its fields to default values, and invokes the selected constructor. this(...) calls another constructor in the same class and must be first; super(...) calls a superclass constructor and must also be first.