0% found this document useful (0 votes)
9 views4 pages

CMP 215 - Object-Oriented Programming Using Java

The document outlines a unit test for CMP 215 – Object-Oriented Programming using Java, consisting of multiple choice questions, short answer questions, descriptive questions, and programming tasks. It covers various Java concepts such as JDK, JRE, JVM, platform independence, and string immutability. The total marks for the test are 50, with specific marks allocated for each section.

Uploaded by

9804427861a
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

CMP 215 - Object-Oriented Programming Using Java

The document outlines a unit test for CMP 215 – Object-Oriented Programming using Java, consisting of multiple choice questions, short answer questions, descriptive questions, and programming tasks. It covers various Java concepts such as JDK, JRE, JVM, platform independence, and string immutability. The total marks for the test are 50, with specific marks allocated for each section.

Uploaded by

9804427861a
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CMP 215 – Object-Oriented Programming

Using Java
UNIT TEST
Full Marks: 50

Section A: Multiple Choice Questions (10


marks)
Each question carries 1 mark.

1.​ Java was originally developed under which project name?​


a) Redwood​
b) Oak​
c) Pine​
d) Maple​

2.​ Which feature of Java ensures platform independence?​


a) Garbage Collection​
b) Bytecode​
c) Encapsulation​
d) Inheritance​

3.​ Which one is NOT a Java Edition?​


a) Java SE​
b) Java EE​
c) Java ME​
d) Java XE​

4.​ Which component actually executes Java bytecode?​


a) JDK​
b) JRE​
c) JVM​
d) Compiler​

5.​ String in Java is:​


a) Mutable​
b) Primitive​
c) Immutable​
d) None​

6.​ Which operator has the highest precedence?​


a) +​
b) *​
c) &&​
d) ==​

7.​ Which loop guarantees at least one execution?​


a) for​
b) while​
c) do-while​
d) none​

8.​ Which keyword is used to stop a loop immediately?​


a) stop​
b) break​
c) exit​
d) continue​

9.​ Which of the following creates a 5-element integer array?​


a) int arr = new int(5);​
b) int[] arr = new int[5];​
c) int arr[] = int[5];​
d) int arr(5);​

10.​Which String method compares content?​


a) ==​
b) equals()​
c) compare()​
d) match()​
Section B: Short Answer Questions (20
marks)
Any 4 questions. Each question carries 5 marks.

11.​Explain the difference between JDK, JRE, and JVM with examples.​

12.​Define platform independence in Java. Explain how bytecode enables WORA (Write
Once Run Anywhere).​

13.​Explain immutability of Strings with suitable examples.​

14.​Write any five major features of Java and explain each briefly.​

15.​Explain the differences between if-else and switch with suitable examples.​

16.​Differentiate between while loop and do-while loop with flow explanation.​

Section C: Descriptive/Long Questions (10


marks)
Any 1 question. Each question carries 10 marks.

17.​

a) Define arrays. Explain the steps to declare, create, initialize, and traverse a 1D array with
examples.​
b) Explain 2D arrays in Java with syntax and example.

OR

18.​

a) Explain method overloading in Java with examples.​


b) Write a user-defined method to calculate the largest of three numbers.​
c) Explain static vs non-static methods.
Section D: Programming Questions (10
marks)
Attempt any 1. Each question carries 10 marks.

19.​Write a Java program to:​

○​ Accept marks of 5 students​

○​ Store them in an array​

○​ Find the highest and lowest marks​

○​ Display all marks​

OR

20.​Write a Java program to:​

○​ Input a string from the user​

○​ Count and print the number of vowels, consonants, digits, and spaces​

○​ Use appropriate string methods​

OR

21.​Write a Java program that defines a method isPrime(int n) and prints all prime
numbers between 1 and 50 using that method.​

TOTAL = 50 MARKS

Common questions

Powered by AI

Java ensures platform independence through its bytecode and JVM architecture. When a program is compiled in Java, it is converted into bytecode, which is a highly optimized set of instructions that can be run on any platform. The Java Virtual Machine (JVM) executes this bytecode. Because JVMs are available for many hardware and software platforms, the same Java bytecode can run anywhere the JVM is present, realizing the principle of 'Write Once, Run Anywhere' (WORA).

Operators in Java perform operations on variables and values. Understanding operator precedence, such as * (multiplication) having higher precedence than + (addition), ensures correct execution order in expressions without unnecessary parentheses. This knowledge helps prevent logical errors, enhances code readability, and ensures reliable program outputs. Misunderstanding precedence, such as applying operations in unintended order, can lead to bugs .

Immutability refers to the property of an object whose state cannot be modified after it is created. In Java, Strings are immutable, meaning once a String is created, it cannot be changed. This property is beneficial for security, synchronization in concurrent programming, and string pool optimization. Mutable objects allow for change, which Strings do not. Immutable Strings help prevent security vulnerabilities and improve performance through optimizations like caching hash codes .

Method overloading in Java allows multiple methods in the same class to have the same name but different parameters (different type, number, or both). This enhances program flexibility and readability because it allows the same operation to be performed with different input types. For example, a method called 'add' can be overloaded to sum two integers, two doubles, or concatenate two Strings, thus allowing appropriate operations based on context .

The main difference between 'while' and 'do-while' loops in Java is that a 'do-while' loop ensures the loop body is executed at least once, as the condition is checked after the loop executes, whereas a 'while' loop checks the condition before executing its body. A 'do-while' loop is favorable in scenarios where the initial iteration must occur regardless of the condition, such as repeat-until logic. For example, prompting a user for input with validation can use a 'do-while' loop to ensure the user is prompted at least once .

Java's object-oriented programming (OOP) offers modular structure, code reusability through inheritance, encapsulation for data hiding, and polymorphism for dynamic method resolution, making it advantageous for robust, maintainable software development. It enables complex problem decomposition, fosters team collaboration through clear structures, and supports scalable applications. These features help manage larger codebases efficiently and promote the DRY (Don't Repeat Yourself) principle .

Java's 'switch' statement is used for precise matching of expressions with fixed values, offering cleaner syntax where many branches apply, such as menus or command-line options. 'If-else' is more suited for evaluating logical expressions, ranges, or complex conditions. Use 'if-else' for evaluating variable conditions or ranges, and 'switch' for multi-way branching based on discrete constant values. For instance, 'if-else' can handle a set of age-based thresholds, while 'switch' efficiently manages selection based on exact day names .

In Java, static methods belong to the class rather than an instance of the class and can be called without creating an object of the class. Non-static methods, on the other hand, belong to an instance of a class, requiring an object for invocation. Static methods can enhance performance and require less memory but lack the flexibility of non-static methods, which can utilize instance variables and participate in polymorphism. Choosing between them depends on whether the operation should affect specific object instances or apply generally across the class .

Arrays in Java can store student marks by declaring an array of integers to hold marks and using indexes for each student's marks. Arrays allow random access of elements, efficient memory management, and ease of performing bulk operations like sorting and searching. Compared to other structures like ArrayLists for fixed-size data sets, arrays offer performance advantages and simpler syntax for basic operations .

To manage data efficiently using a 1D array in Java, follow these steps: 1) Declare the array with a data type, e.g., 'int[] array;'. 2) Create the array with a fixed size, e.g., 'array = new int[5];'. 3) Initialize the array elements individually or through a loop. 4) Traverse using a loop to access and process each element. This structured approach allows efficient indexing and memory management, making arrays a fundamental structure for storing and manipulating data collections .

You might also like