Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

2CEIT302:

Object Oriented Programming Assignment -1

Q-1 Discuss Java Architecture in detail.

• JVM(Java Virtual Machine)

• JVM (Java Virtual Machine) is an abstract machine. It is a specification that


provides runtime environment in which java bytecode can be executed.
• JVMs are available for many hardware and software platforms (i.e. JVM is
platform dependent).
• A specification where working of Java Virtual Machine is specified. But
implementation provider is independent to choose the algorithm. Its
implementation has been provided by Oracle and other companies.
• An implementation Its implementation is known as JRE (Java Runtime
Environment).
• Runtime Instance Whenever you write java command on the command
prompt to run the java class, an instance of JVM is created.

• JRE

• JRE (Java Runtime Environment). It is also written as Java RTE. The Java
Runtime environment is a set of software tools which are used for
developing Java applications. It is used to provide the runtime
environment. It is the implementation of JVM. It physically exists. It
contains a set of libraries + other files that JVM uses at runtime.

• JDK(Java Development Kit).

• JDK (Java Development Kit). The Java Development Kit (JDK) is a software
development environment which is used to develop java applications and
applets. It contains JRE + development tools.
Q-2 Differentiate between For Loop and Enhanced For Loop with
Java Program

• FOR LOOP

• The Java for loop is used to iterate a part of the program several times. If
the number of iteration is fixed, it is recommended to use for loop.
• In for loop we put initialization, condition and increment/decrement all
together. Initialization will be done once at the beginning of loop.
• Then, the condition is checked by the compiler. If the condition is false,
for loop is terminated.
• But, if condition is true then, the statements are executed until condition
is false.
Syntax:
for(initialization;condition;incr/decr)
{
//statement or code to be executed
}
EXAMPLE:-
class Example {
public static void main(String[] args) {
for(int i=1;i<=5;i++)
{
System.out.println(i);
}
}
}

Output:
1
2
3
4
5

• Java For-each Loop | Enhanced For Loop

• The Java for-each loop or enhanced for loop is introduced since


J2SE 5.0.
• It provides an alternative approach To traverse the array or
collection in Java.
• It is mainly used to traverse the array or collection elements.
• The advantage of the for-each loop is that it eliminates the
possibility of bugs and makes the code more readable.
• It is known as the for-each loop because it traverses each
element one by one.
• The drawback of the enhanced for loop is that it cannot traverse
the elements in reverse order.
• Here, you do not have the option to skip any element because
it does not work on an index basis.
• The Java for-each loop traverses the array or collection until the
last element.
• For each element, it stores the element in the variable and
executes the body of the for-each loop.

EXAMPLE:-
class Example1
{
public static void main(String args[])
{
//declaring an array
int arr[]={12,13,14,44};
//traversing the array with for- each loop
for(int i:arr)
{
System.out.println(i);
}
}
}
Output:
12
13
14
44

Q-3 Write the output and justification for following code:


(1)
public class Solution{
public static void main(String[] args){
byte x = 127;
x++; x++;
System.out.print(x);}}

Output:-
-127
Justification:-
• ‘X’ is initially set to 127.
• It is then incremented twice with x++, which makes it 129.
• Finally, System.out.print(x) prints the value 129 to the console.

(2)
public class Solution{
public static void main(String[] args){
int[] x = {120, 200, 016};
for(int i = 0; i < x.length; i++){
System.out.print(x[i] + “ “)}}}

Output:-
120 200 14
Justification:-
• The corrected code prints the elements of the x array with spaces
between them.
• The array contains three elements: 120, 200, and 14 (because 016 is
treated as octal, which is equivalent to 14 in decimal).
• The for loop iterates through the array and prints each element,
separated by a space, resulting in the output 120 200 14.

(3)
public class Solution{
public static void main(String[] args){
int[] A = {0,2,4,1,3};
for(int i = 0; i < a.length; i++){
a[i] = a[(a[i] + 3) % a.length];}}}
output:-
Compilation error

Justification:-
• The printing statement in the following program is missing and therefore
the integer delcare in for loop will not be printed.
• Therefore it is considere to be compilation error.

(4)
public class Solution{
public static void main(String[] args){
String str = “abcde”;
System.out.println(str.substring(1, 3));}}

Output:-
bc

Justification:-
• The substring method extracts the characters from index 1 (inclusive) to
index 3 (exclusive) of the string "abcde", which are "bc".
• The System.out.println statement prints this substring, resulting in the
output "bc".

Q-4 What do you mean by Jagged array? How to declare and


initialize jagged array? Explain with Java Program.
• A jagged array, also known as an array of arrays, is a two-
dimensional array in which each row can have a different length.
• Unlike a regular two-dimensional array, where all rows have the
same number of columns, a jagged array allows for varying row
lengths.
• This flexibility can be useful in situations where you need to
represent data with irregular or non-uniform structures.

To declare and initialize a jagged array in Java,
• Declare the array: You declare an array of arrays using square
brackets. The first set of brackets specifies the number of rows,
and the second set of brackets specifies the number of columns
(though this is optional for jagged arrays since rows can have
different lengths).
• Initialize each row individually: For each row of the jagged array,
you need to initialize it as a separate array with its own length.

EXAMPLE:-
public class array4 {
public static void main(String[] args) {

int[][] a = new int[3][];

a[0] = new int[] {1, 2, 3};


a[1] = new int[] {4, 5};
a[2] = new int[] {6, 7, 8, 9};
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
Output:-
123
45
6789

• we declared a jagged array jaggedArray with three rows, but each row has
a different number of columns. We then initialized each row individually
with different lengths to create a jagged structure. Finally, we used nested
loops to access and print the elements of the jagged array.

Q-5 Discuss String and StringBuffer in detail with Java Program.

• String is a sequence of characters. But in Java, string is an object that


represents a sequence of characters.
• The java.lang.String class is used to create a string object.
• Strings are the type of objects that can store the character of values and
in Java, every character is stored in 16 bits i,e using UTF 16-bit encoding.
A string acts the same as an array of characters in Java.
• There are two ways to create String object:
• (1) By string literal (2) By new keyword
• Java String literal is created by using double quotes. For Example:
String s="welcome";
• Each time you create a string literal, the JVM checks the "string constant
pool" first. If the string already exists in the pool, a reference to the pooled
instance is returned. If the string doesn't exist in the pool, a new string
instance is created and placed in the pool.
EXAMPLE:-
public class StringExample {
public static void main(String args[])
{
String str = new String("example");
System.out.println(str);
}
}
OUTPUT:-
Sexample

➢ StringBuffer class

• StringBuffer class is used to create a mutable string object. It means, it can


be changed after it is created. It represents growable and writable
character sequence.
• It contains some particular sequence of characters, but the length and
content of the sequence can be changed through certain method calls.

➢ Some methods of the StringBuffer class:


• StringBuffer objects are mutable, meaning that you can change the
contents of the buffer without creating a new object.
• The initial capacity of a StringBuffer can be specified when it is created, or
it can be set later with the ensureCapacity() method.
• The append() method is used to add characters, strings, or other objects
to the end of the buffer.
• The insert() method is used to insert characters, strings, or other objects
at a specified position in the buffer.
• The delete() method is used to remove characters from the buffer.
• The reverse() method is used to reverse the order of the characters in the
buffer.

EXAMPLE:-

public class StringBufferExample {


public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
sb.append("Hello");
sb.append(" ");
sb.append("world");
String message = sb.toString();
System.out.println(message);
}
}
OUTPUT:-
Hello world

You might also like