You are on page 1of 10

Module-2 Questions & Solutions

1. Discuss briefly the concept of byte code in Java. July-18, Marks 4

Bytecode is an intermediate form of Java programs. Bytecode consists of an


optimized set of instructions that are not specific to the processor. Java programs are
compiled using the javac compiler to get bytecode.

The bytecode is executed using a Java runtime environment which is called a Java
Virtual Machine (JVM). Sometimes JVM is also called an interpreter for bytecode.

The programs that are running on JVM must be compiled into a binary format which is
denoted by .class files.

Sometime for ease of distribution multiple class files are packaged into one jar file.

The JVM executes .class or .jar files, by either interpreting it or using a just-in-time
compiler (JIT) .

The JIT is used for compiling and not for interpreting the file. It is used in most JVMs
today to achieve greater speed.

The bytecode verifier verifies all the bytecode before it is executed. This verification
helps to prevent the crashing of the host machine.

Java Program Execution Process


Components of Java Development Kit (JDK)

The Java Development Kit (JDK) is a collection of tools that are used to develop,
compile, and run Java programs. Java Development Kit consists of –

javac – The javac is the Java compiler that translates the Java source code into
bytecode form and stores it in a separate class file.

java – The java in the Java interpreter, which interprets the bytecode stored in the
class file and executes the program to generate output.

javadoc – The javadocis used to convert the Java source code into HTML document
for documentation from source code file.

javah – The javah is used to produce the header files for the use of native methods.

jdb – The jdb is the Java debugger which is used to locate the errors in the program.

appletviewer – The apletviewr is used for executing the Java applet.


Following are the steps that illustrate the execution process of the Java
application program

First, user creates the Java source code using any text editor like Notepad, Notepad++
etc.

Next, the source code is compiled using the javac compiler. The javac compiler
converts the source program into a class file which consists of the byte code.

The developer can use javadoc tool to create the HTML files that document the source
program.

The developer may use javah tool for creating the header files.

Finally, the java tool is used to interpret the class file generated by javac tool in order to
produce an executable.
2. Write a Java Program to Find the Sum of array elements, Product of array
elements, and Average of elements of a single dimensional array.

Steps (Program logic):

1. Declare the local variables like a, sum, prod, avg to store array elements, the sum of
the array, a product of array, and an average of array elements respectively.

2. Read the number of elements in an array.

3. Instantiate the array with the number of array elements.

4. Read the elements one by one and store in array.

5. Find the sum, product of elements of an array, then find the average using the sum
and number of elements in an array.

6. Finally, display the sum, product and average of elements of an array.

Source code of Java Program to find Sum, Product, and Avg. of an Array

import java.util.Scanner;
/*
* Java Program to find the Sum, Product and Average of
elements single
* dimensional array

* Subscribe to Mahesh Huddar YouTube Channel for more


Videos
*/
public class ArraySumProductAvg
{
public static void main(String args[])
{
int a[], sum = 0, prod = 1, avg, num;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of array
elements:");
num = in.nextInt();
a = new int[num];
System.out.println("Enter the array elements: ");
for (int i = 0; i < num; i++)
{
System.out.println("Enter the "+(i+1)+"
element:");
a[i] = in.nextInt();
}
for (int i = 0; i < num; i++)
{
sum = sum + a[i];
prod = prod * a[i];
}
avg = sum / num;
System.out.println("Sum of array elements is:
"+sum);
System.out.println("Product of array elements is:
"+prod);
System.out.println("Average of array elements is:
"+avg);
}
}

Output:

Enter the number of array elements: 5

Enter the array elements:

Enter the 1 element: 5

Enter the 2 element: 10

Enter the 3 element: 15

Enter the 4 element: 20


Enter the 5 element: 25

Sum of array elements is: 75

Product of array elements is: 375000

Average of array elements is: 15

3. Write a Java program to print first ‘n’ Fibonacci numbers.


Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13 and so on

If the value of n is 5, then the expected output is 0, 1, 1, 2, 3

Logic is: f1 = 0, f2 = 1, f3 = f1 + f2

Steps (Program logic):

1. Declare variables like n, f1, f2, f3.

2. Create an object of Scanner class to read the input from standard keyboard.

3. Check the value of n , if the value of n is 1 display f1 that is 0.

4. If the value of n is 2, display f1 and f2 that is 0 and 1.

5. If the value of n is greater than 2 then display f1, f2 and calculate the next number in
the series using formula f3=f1+f2.

6. if the value of n is less than or equal 0 then display that the error message like
invalid input.

Source code of Java Program to print first “n” Numbers in Fibonacci Series

import java.util.Scanner;
/*
Write a Java Program to display first "n" numbers in
Fibonacci series
Subscribe to Mahesh Huddar YouTube Channel for more Videos
Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13 and so on
Logic: f1 = 0, f2 = 1, f3 = f1 + f2
*/
public class Fibonacci
{
public static void main(String args[])
{
int n, f1 = 0, f2 = 1, f3;
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of n:");
n = input.nextInt();
System.out.print("Fibonacci series is: ");
if (n == 1)
{
System.out.print(f1);
}
else if (n == 2)
{
System.out.print(f1 +" "+ f2);
}
else if (n > 2)
{
System.out.print(f1 +" "+ f2+" ");
for (int i = 3; i<=n; i++)
{
f3 = f1 + f2;
System.out.print(f3+" ");
f1 = f2;
f2 = f3;
}
}
else
{
System.out.print("Invalid Input - Enter the
value of n greater than 0");
}
}
}
Output:

Case 1:

Enter the value of n: 1

Fibonacci series is: 0

Case 2:

Enter the value of n: 2

Fibonacci series is: 0 1

Case 3:

Enter the value of n: 5

Fibonacci series is: 0 1 1 2 3

Case 4:

Enter the value of n: -10

Fibonacci series is: Invalid Input – Enter the value of n greater than 0

5. Write a Java program to find the number of and the sum of all numbers
greater than 100 and less than 200 which are divisible by 7.

Steps (Program logic):

1. Declare two local variables sum and count both are set to 0 to store the sum of all
numbers between 100 and 200 which are divisible by 7 and count of those numbers
respectively.
2. Use a for loop a to loop over from 101 to 199.

3. Check all the numbers between 100 to 200, whether they are divisible by 7 using
mod operator.

4. If number is divisible by 7, then add number to previous sum and increment the
count.

5. after the loop, display the result, that is Sum of number between 100 to 200 which
are divisible by 7 and count of numbers between 100 to 200 which are divisible by 7.

Source code of Java Program to find the sum of numbers divisible by 7 between
100 and 200

/*
* Write a Java program to find the number of and the sum
of all
* numbers greater than 100 and less than 200 which are
divisible by 7

* Subscribe to Mahesh Huddar YouTube Channel for more


Videos
*/
public class SumCountDivisibleBy7
{
public static void main(String args[])
{
int sum = 0, count = 0;
for (int i = 101; i < 200; i++)
{
if (i % 7 == 0)
{
sum = sum + i;
count++;
}
}
System.out.println("The Sum of the number between 100 to
200 which are divisible by 7 is: "+sum);
System.out.println("Total numbers between 100 to 200 which
are divisible by 7 is: "+count);
}
}

Output:

The Sum of the number between 100 to 200 which are divisible by 7 is: 2107

Total numbers between 100 to 200 which are divisible by 7 is: 14

You might also like