Skip to main content

Chapter 4

Object Oriented Programming with Java

TU BIM past exam questions

Past questions and answers

22 questions set from this chapter. Most repeated first.

  • 2023 AD Model · 1 mark

What is bytecode?

Answer

Bytecode is the platform-independent, intermediate code produced by the Java compiler (javac) from source code and stored in .class files. The Java Virtual Machine (JVM) interprets or JIT-compiles it into machine code, which makes Java programs portable ("write once, run anywhere").

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 1 mark

Differentiate between break and continue.

Answer

  • break terminates the enclosing loop (or switch) completely and transfers control to the statement after it.
  • continue skips the rest of the current iteration and moves on to the next iteration of the loop.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 1 mark

What do you mean by associativity of an operator?

Answer

Associativity decides the order in which operators of the same precedence are evaluated in an expression. Most binary operators are left-to-right (e.g. a - b - c is (a - b) - c), whereas assignment and unary operators are right-to-left (e.g. a = b = c is a = (b = c)).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 1 mark

How two-dimensional array is declared in Java?

Answer

A two-dimensional array is an array of arrays and is declared as:

int[][] a = new int[3][4];          // 3 rows, 4 columns
int[][] b = {{1, 2}, {3, 4}};       // declaration with initialization

Elements are accessed as a[i][j].

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 1 mark

Why constructor is needed?

Answer

A constructor is needed to initialize the instance variables of an object at the moment it is created, so the object starts in a valid state. It has the same name as the class and no return type, and it is invoked automatically by new. Constructors can be overloaded to create objects with different initial values.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 1 mark

What is garbage collection in Java?

Answer

Garbage collection is the automatic process by which the JVM finds objects that are no longer reachable by any reference and reclaims their memory. The programmer need not free memory explicitly, which prevents most memory leaks. It can be suggested with System.gc(), and an object may run its finalize() method before it is collected.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 1 mark

What do you mean by dynamic method dispatch?

Answer

Dynamic method dispatch is the run-time mechanism by which a call to an overridden method is resolved according to the actual object type, not the reference type. When a superclass reference refers to a subclass object, the subclass's version of the method is executed. It implements run-time polymorphism.

class A { void show() { System.out.println("A"); } }
class B extends A { void show() { System.out.println("B"); } }
A r = new B();
r.show();   // prints B

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 1 mark

What is the difference between final and finally keyword?

Answer

finalfinally
A keyword/modifier: a final variable cannot be reassigned, a final method cannot be overridden, a final class cannot be inherited.A block following try/catch that always executes, whether or not an exception occurs.
Used in declarations.Used for clean-up such as closing files.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 1 mark

What is wildcard in java?

Answer

The wildcard is the question mark ? used in generics to represent an unknown type. Forms: unbounded List<?>, upper bounded List<? extends Number> (any subclass of Number), and lower bounded List<? super Integer> (any superclass of Integer). It lets a method accept a generic type with different type arguments.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 1 mark

Define autoboxing with example.

Answer

Autoboxing is the automatic conversion of a primitive value into its corresponding wrapper class object by the compiler (the reverse is unboxing).

int x = 10;
Integer obj = x;      // autoboxing: int -> Integer
int y = obj;          // unboxing: Integer -> int

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 3 marks

Write a program to demonstrate the use of logical OR, AND, and NOT operator with suitable example.

Answer

public class LogicalDemo {
    public static void main(String[] args) {
        int age = 25;
        boolean hasId = true;

        // AND: both conditions must be true
        if (age >= 18 && hasId)
            System.out.println("AND: Allowed to vote");

        // OR: at least one condition must be true
        if (age < 18 || hasId)
            System.out.println("OR: Has ID or is a minor");

        // NOT: reverses the result
        boolean isMinor = age < 18;
        System.out.println("NOT: Adult = " + !isMinor);
    }
}

Output:

AND: Allowed to vote
OR: Has ID or is a minor
NOT: Adult = true

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 3 marks

Write a java program to find the second largest element in an array.

Answer

public class SecondLargest {
    public static void main(String[] args) {
        int[] a = {12, 35, 1, 10, 34, 1};
        int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;

        for (int n : a) {
            if (n > first) {
                second = first;
                first = n;
            } else if (n > second && n != first) {
                second = n;
            }
        }

        if (second == Integer.MIN_VALUE)
            System.out.println("No second largest element");
        else
            System.out.println("Second largest = " + second);
    }
}

Output: Second largest = 34

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 3 marks

Write a java program to illustrate the concepts of method overloading.

Answer

Method overloading means several methods in a class share the same name but differ in the number or types of parameters.

public class Calc {
    int add(int a, int b)          { return a + b; }
    int add(int a, int b, int c)   { return a + b + c; }
    double add(double a, double b) { return a + b; }

    public static void main(String[] args) {
        Calc c = new Calc();
        System.out.println(c.add(2, 3));        // 5
        System.out.println(c.add(2, 3, 4));     // 9
        System.out.println(c.add(2.5, 3.5));    // 6.0
    }
}

The compiler selects the method at compile time from the arguments (compile-time polymorphism).

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 3 marks

Write a program by using generic method to swap the positions of two different elements in an array.

Answer

public class GenericSwap {
    static <T> void swap(T[] arr, int i, int j) {
        T temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        Integer[] nums = {1, 2, 3, 4};
        String[] names = {"Ram", "Sita", "Hari"};

        swap(nums, 0, 3);
        swap(names, 1, 2);

        System.out.println(java.util.Arrays.toString(nums));   // [4, 2, 3, 1]
        System.out.println(java.util.Arrays.toString(names));  // [Ram, Hari, Sita]
    }
}

The same generic method works for arrays of any reference type.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 3 marks

Write a java program to find the factorial of any positive integer given by user using recursion.

Answer

import java.util.Scanner;

public class Factorial {
    static long fact(int n) {
        if (n == 0 || n == 1)
            return 1;               // base case
        return n * fact(n - 1);     // recursive call
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        int n = sc.nextInt();
        System.out.println("Factorial of " + n + " = " + fact(n));
    }
}

For input 5 the output is Factorial of 5 = 120.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 3 marks

Explain for-each statement with suitable example.

Answer

The for-each (enhanced for) statement iterates over every element of an array or collection without using an index.

Syntax:

for (datatype variable : arrayOrCollection) {
    // statements
}

Example:

int[] marks = {70, 85, 60};
int sum = 0;
for (int m : marks) {
    System.out.println(m);
    sum += m;
}
System.out.println("Sum = " + sum);   // 215

It is shorter and avoids index errors, but it cannot modify array elements through the loop variable or go backwards.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 5 marks

Create a Shape interface having methods area() and perimeter(). Create two subclasses, Circle and Rectangle that implement the Shape interface. Create a class Sample with main method and demonstrate the area and perimeters of both the shape classes. You need to handle the values of length, breadth, and radius in respective classes to calculate their area and perimeter.

Answer

interface Shape {
    double area();
    double perimeter();
}

class Circle implements Shape {
    private double radius;
    Circle(double radius) { this.radius = radius; }
    public double area()      { return Math.PI * radius * radius; }
    public double perimeter() { return 2 * Math.PI * radius; }
}

class Rectangle implements Shape {
    private double length, breadth;
    Rectangle(double length, double breadth) {
        this.length = length;
        this.breadth = breadth;
    }
    public double area()      { return length * breadth; }
    public double perimeter() { return 2 * (length + breadth); }
}

public class Sample {
    public static void main(String[] args) {
        Shape c = new Circle(7);
        Shape r = new Rectangle(5, 3);

        System.out.println("Circle area = " + c.area());
        System.out.println("Circle perimeter = " + c.perimeter());
        System.out.println("Rectangle area = " + r.area());
        System.out.println("Rectangle perimeter = " + r.perimeter());
    }
}

Output: circle area about 153.94 and perimeter about 43.98; rectangle area 15.0 and perimeter 16.0.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 5 marks

Create a class Student with private member variables name and percentage. Write methods to set, display, and return values of private member variables in the Student class. Create 10 different objects of the Student class, set the values, and display name of the Student who have highest average_marks in the main method of another class named StudentDemo.

Answer

class Student {
    private String name;
    private double percentage;

    public void setData(String name, double percentage) {
        this.name = name;
        this.percentage = percentage;
    }
    public void display() {
        System.out.println(name + " : " + percentage + "%");
    }
    public String getName()        { return name; }
    public double getPercentage()  { return percentage; }
}

public class StudentDemo {
    public static void main(String[] args) {
        String[] names = {"Ram", "Sita", "Hari", "Gita", "Shyam",
                          "Rita", "Bimal", "Anita", "Suman", "Maya"};
        double[] pct   = {65, 78.5, 82, 91.2, 55, 70, 88, 67.5, 74, 85};

        Student[] s = new Student[10];
        for (int i = 0; i < 10; i++) {
            s[i] = new Student();
            s[i].setData(names[i], pct[i]);
            s[i].display();
        }

        Student top = s[0];
        for (int i = 1; i < 10; i++)
            if (s[i].getPercentage() > top.getPercentage())
                top = s[i];

        System.out.println("Highest average: " + top.getName());
    }
}

Output ends with Highest average: Gita.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 5 marks

Differentiate between Checked and Unchecked Exceptions. Write a program to illustrate the concept of ArrayIndexOutOfBoundException.

Answer

Checked exceptionsUnchecked exceptions
Checked by the compiler at compile time.Not checked at compile time; occur at run time.
Must be handled with try-catch or declared with throws.Handling is optional.
Subclasses of Exception other than RuntimeException (e.g. IOException, SQLException).Subclasses of RuntimeException and Error (e.g. ArithmeticException, ArrayIndexOutOfBoundsException).
Usually external conditions.Usually programming errors.

ArrayIndexOutOfBoundsException program

public class IndexDemo {
    public static void main(String[] args) {
        int[] a = new int[5];
        try {
            a[5] = 10;      // valid indices are 0 to 4
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Exception caught: " + e);
        } finally {
            System.out.println("Program finished");
        }
    }
}

Output:

Exception caught: java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
Program finished

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 5 marks

Write a Java program to read data from the file "text.txt" and write the data into the file "best.txt".

Answer

import java.io.*;

public class CopyFile {
    public static void main(String[] args) {
        try (FileReader fr = new FileReader("text.txt");
             FileWriter fw = new FileWriter("best.txt")) {

            int ch;
            while ((ch = fr.read()) != -1) {
                fw.write(ch);
            }
            System.out.println("File copied successfully");

        } catch (FileNotFoundException e) {
            System.out.println("text.txt not found");
        } catch (IOException e) {
            System.out.println("I/O error: " + e.getMessage());
        }
    }
}

The try-with-resources statement closes both streams automatically.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 10 marks

Explain the OOP principal followed by Java. What are the restrictions when the method is declared as static? Write a java program to illustrate the concepts of static method.

Answer

OOP principles followed by Java

  1. Encapsulation: binding data and the methods that operate on it into a class, and hiding data with access modifiers (private fields with public getters/setters).
  2. Inheritance: a subclass acquires the fields and methods of a superclass using extends, giving code reuse.
  3. Polymorphism: one name, many forms, either compile-time (method overloading) or run-time (method overriding with dynamic dispatch).
  4. Abstraction: exposing only essential behaviour and hiding implementation, using abstract classes and interfaces.

(Java also uses classes and objects as its basic building blocks.)

Restrictions on static methods

  • A static method can directly call only other static methods.
  • It can directly access only static data members; non-static members need an object reference.
  • It cannot use this or super.
  • It cannot be overridden (only hidden) and cannot be abstract.
  • It belongs to the class and can be called without creating an object, as ClassName.method().

Program

public class StaticDemo {
    static int count = 0;
    int id = 5;

    static void increment() {          // static method
        count++;
        System.out.println("Count = " + count);
        // System.out.println(id);     // error: non-static variable
    }

    static int square(int n) { return n * n; }

    public static void main(String[] args) {
        StaticDemo.increment();
        increment();
        System.out.println("Square = " + square(6));
        StaticDemo obj = new StaticDemo();
        System.out.println("id via object = " + obj.id);
    }
}

Output:

Count = 1
Count = 2
Square = 36
id via object = 5

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

  • 2023 AD Model · 10 marks

What is inheritance? What are the advantages of using inheritance? Explain different types of inheritance with example of each.

Answer

Inheritance is the mechanism by which a class (subclass/child) acquires the properties and methods of another class (superclass/parent) using the extends keyword. It models an "is-a" relationship.

Advantages

  • Code reusability: common code is written once in the superclass.
  • Method overriding and run-time polymorphism are possible.
  • Easier maintenance and extension.
  • Natural hierarchical classification of real-world entities.
  • Reduced development time and redundancy.

Types of inheritance

(a) Single: one subclass inherits from one superclass.

class Animal { void eat() { System.out.println("eating"); } }
class Dog extends Animal { void bark() { System.out.println("barking"); } }

(b) Multilevel: a class inherits from a class which itself inherits from another (A -> B -> C).

class Puppy extends Dog { void weep() { System.out.println("weeping"); } }

Puppy can use eat(), bark() and weep().

(c) Hierarchical: several subclasses inherit from one superclass.

class Cat extends Animal { void meow() { System.out.println("meow"); } }

Dog and Cat both extend Animal.

(d) Multiple: a class inherits from more than one parent. Java does not support this with classes (to avoid the diamond problem) but achieves it through interfaces.

interface Printable { void print(); }
interface Showable { void show(); }
class Demo implements Printable, Showable {
    public void print() { System.out.println("print"); }
    public void show()  { System.out.println("show"); }
}

(e) Hybrid: a combination of two or more of the above; in Java it is possible only through interfaces.

Answer checked by Claude Sonnet · Oct 21, 2026

Test yourself · 2 MCQs

Loading…

Questions from TU BIM Business Communication (MGT 204) 2001 AD (Board exam paper, 2001 AD), TU BIM Business Communication (MGT 204) 2002 AD (Board exam paper, 2002 AD), TU BIM Business Communication (MGT 204) 2003 AD (Board exam paper, 2003 AD), TU BIM Business Communication (MGT 204) 2005 AD (Board exam paper, 2005 AD), TU BIM Business Communication (MGT 204) 2007 AD (Board exam paper, 2007 AD), TU BIM Business Communication (MGT 204) 2008 AD (Board exam paper, 2008 AD), TU BIM Business Communication (MGT 204) 2009 AD (Board exam paper, 2009 AD), TU BIM Business Communication (MGT 204) 2010 AD (Board exam paper, 2010 AD), TU BIM Business Communication (MGT 204) 2011 AD (Board exam paper, 2011 AD), TU BIM Business Communication (MGT 204) 2012 AD (Board exam paper, 2012 AD), TU BIM Business Communication (MGT 204) 2013 AD (Board exam paper, 2013 AD), TU BIM Business Communication (MGT 204) 2014 AD (Board exam paper, 2014 AD), TU BIM Business Communication (MGT 204) 2015 AD (Board exam paper, 2015 AD), TU BIM Business Communication (MGT 204) 2016 AD (Board exam paper, 2016 AD), TU BIM Business Communication (MGT 204) 2017 AD (Board exam paper, 2017 AD), TU BIM Business Communication (MGT 204) 2018 AD (Board exam paper, 2018 AD), TU BIM Business Communication (MGT 204) 2019 AD (Board exam paper, 2019 AD), TU BIM Business Communication (MGT 204) 2021 AD (Board exam paper, 2021 AD), TU BIM Discrete Mathematics (MTH 202) 2005 AD (Board exam paper, 2005 AD), TU BIM Discrete Mathematics (MTH 202) 2007 AD (Board exam paper, 2007 AD), TU BIM Discrete Mathematics (MTH 202) 2008 AD (Board exam paper, 2008 AD), TU BIM Discrete Mathematics (MTH 202) 2009 AD (Board exam paper, 2009 AD), TU BIM Discrete Mathematics (MTH 202) 2010 AD (Board exam paper, 2010 AD), TU BIM Discrete Mathematics (MTH 202) 2011 AD (Board exam paper, 2011 AD), TU BIM Discrete Mathematics (MTH 202) 2012 AD (Board exam paper, 2012 AD), TU BIM Discrete Mathematics (MTH 202) 2013 AD (Board exam paper, 2013 AD), TU BIM Discrete Mathematics (MTH 202) 2014 AD (Board exam paper, 2014 AD), TU BIM Discrete Mathematics (MTH 202) 2016 AD (Board exam paper, 2016 AD), TU BIM Discrete Mathematics (MTH 202) 2017 AD (Board exam paper, 2017 AD), TU BIM Discrete Mathematics (MTH 202) 2018 AD (Board exam paper, 2018 AD), TU BIM Discrete Mathematics (MTH 202) 2019 AD (Board exam paper, 2019 AD), TU BIM Discrete Mathematics (MTH 202) 2021 AD (Board exam paper, 2021 AD), TU BIM Data Communication and Computer Network (ITC 214) 2001 AD (Board exam paper, 2001 AD), TU BIM Data Communication and Computer Network (ITC 214) 2002 AD (Board exam paper, 2002 AD), TU BIM Data Communication and Computer Network (ITC 214) 2004 AD (Board exam paper, 2004 AD), TU BIM Data Communication and Computer Network (ITC 214) 2005 AD (Board exam paper, 2005 AD), TU BIM Data Communication and Computer Network (ITC 214) 2009 AD (Board exam paper, 2009 AD), TU BIM Data Communication and Computer Network (ITC 214) 2010 AD (Board exam paper, 2010 AD), TU BIM Data Communication and Computer Network (ITC 214) 2011 AD (Board exam paper, 2011 AD), TU BIM Data Communication and Computer Network (ITC 214) 2012 AD (Board exam paper, 2012 AD), TU BIM Data Communication and Computer Network (ITC 214) 2013 AD (Board exam paper, 2013 AD), TU BIM Data Communication and Computer Network (ITC 214) 2014 AD (Board exam paper, 2014 AD), TU BIM Data Communication and Computer Network (ITC 214) 2015 AD (Board exam paper, 2015 AD), TU BIM Data Communication and Computer Network (ITC 214) 2016 AD (Board exam paper, 2016 AD), TU BIM Data Communication and Computer Network (ITC 214) 2017 AD (Board exam paper, 2017 AD), TU BIM Data Communication and Computer Network (ITC 214) 2018 AD (Board exam paper, 2018 AD), TU BIM Data Communication and Computer Network (ITC 214) 2019 AD (Board exam paper, 2019 AD), TU BIM Data Communication and Computer Network (ITC 214) 2021 AD (Board exam paper, 2021 AD), TU BIM Sociology for Business (SOC 201) 2001 AD (Board exam paper, 2001 AD), TU BIM Sociology for Business (SOC 201) 2002 AD (Board exam paper, 2002 AD), TU BIM Sociology for Business (SOC 201) 2003 AD (Board exam paper, 2003 AD), TU BIM Sociology for Business (SOC 201) 2005 AD (Board exam paper, 2005 AD), TU BIM Sociology for Business (SOC 201) 2008 AD (Board exam paper, 2008 AD), TU BIM Sociology for Business (SOC 201) 2009 AD (Board exam paper, 2009 AD), TU BIM Sociology for Business (SOC 201) 2011 AD (Board exam paper, 2011 AD), TU BIM Sociology for Business (SOC 201) 2012 AD (Board exam paper, 2012 AD), TU BIM Sociology for Business (SOC 201) 2013 AD (Board exam paper, 2013 AD), TU BIM Sociology for Business (SOC 201) 2014 AD (Board exam paper, 2014 AD), TU BIM Sociology for Business (SOC 201) 2015 AD (Board exam paper, 2015 AD), TU BIM Structured Programming (ITC 213) 2001 AD (Board exam paper, 2001 AD), TU BIM Structured Programming (ITC 213) 2002 AD (Board exam paper, 2002 AD), TU BIM Structured Programming (ITC 213) 2004 AD (Board exam paper, 2004 AD), TU BIM Structured Programming (ITC 213) 2007 AD (Board exam paper, 2007 AD), TU BIM Structured Programming (ITC 213) 2008 AD (Board exam paper, 2008 AD), TU BIM Structured Programming (ITC 213) 2009 AD (Board exam paper, 2009 AD), TU BIM Structured Programming (ITC 213) 2010 AD (Board exam paper, 2010 AD), TU BIM Structured Programming (ITC 213) 2012 AD (Board exam paper, 2012 AD), TU BIM Structured Programming (ITC 213) 2002 AD (Objective question paper, 2002 (20 MCQs, 0.5 marks each, full marks 10)), TU BIM Structured Programming (ITC 213) 2003 AD (Objective question paper, 2003 (20 MCQs, 0.5 marks each, full marks 10); item 10 as printed in the scan), TU BIM Structured Programming (ITC 213) 2013 AD (Board exam paper, 2013 (the scan of this paper ends after question 5)), TU BIM Structured Programming (ITC 213) 2014 AD (Board exam paper, 2014 AD), TU BIM Structured Programming (ITC 213) 2015 AD (Board exam paper, 2015 (the line "if( (m 2 ) == 0" is printed with a modulus sign that is illegible in the scan; read as m % 2)), TU BIM Structured Programming (ITC 213) 2016 AD (Board exam paper, 2016 (the first figure in question 2 is transcribed as printed)), TU BIM Structured Programming (ITC 213) 2017 AD (Board exam paper, August-September 2017), TU BIM Structured Programming (ITC 213) 2018 AD (Board exam paper, September-October 2018), TU BIM Structured Programming (ITC 213) 2021 Aug (Board exam paper, August 2021) and TU BIM Second Semester Model Questions 2022/23 (IT 232, IT 233, IT 235, IT 234, MGT 241) (Faculty of Management model question set, BIM second semester (new course), via EducateNepal). Answers are written for this site; check them against your class notes.