Professional Documents
Culture Documents
SWET OOP Ass.1 1
SWET OOP Ass.1 1
• 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). 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
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
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".
EXAMPLE:-
public class array4 {
public static void main(String[] args) {
• 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.
➢ StringBuffer class
EXAMPLE:-