School of Informatics and Electrical Engineering: Hachalu Hundessa Campus
School of Informatics and Electrical Engineering: Hachalu Hundessa Campus
School of Informatics and Electrical Engineering: Hachalu Hundessa Campus
Engineering
Project
ID.No: BET/1259/11
Answer:
JRE Components
The JRE is the software environment in which programs compiled for a typical JVM
implementation can run. The runtime system includes:
Code necessary to run Java programs, dynamically link native methods, manage
memory, and handle exceptions
Implementation of the JVM
The following figure shows the JRE and its components, including a typical JVM
implementation's various modules and its functional position with respect to the JRE and
class libraries.
JVM
The JVM is an abstract computing machine, having an instruction set that uses memory.
Virtual machines are often used to implement a programming language. The JVM is the
cornerstone of the Java programming language. It is responsible for Java's cross-platform
portability and the small size of its compiled code.
The Solaris JVM is used to execute Java applications. The Java compiler, javac, outputs byte
codes and puts them into a .class file. The JVM then interprets these bytecodes, which can
then be executed by any JVM implementation, thus providing Java's cross-platform
portability. The next two figures illustrate the traditional compile-time environment and the
new portable Java compile-time environment.
The Sun Java JIT compiler, an integral part of the Solaris JVM, can accelerate execution
performance many times over previous levels. Long-running, compute-intensive programs
show the best performance improvement.
When the JIT compiler environment variable is on (the default), the JVM reads the .class file
for interpretation and passes it to the JIT compiler. The JIT compiler then compiles the
bytecodes into native code for the platform on which it is running. The next figure illustrates
the JIT compile process.
Answer: D
JIT has the capability to combine the advantages found both in interpretation and static (that
is to say ahead of time) compilation. As an interpreter, JIT is able to improve performance
through caching results of blocks of code that has been translated – compared to simply re-
evaluating every line or operand in the code each time that it occurs (as in interpreted
language). Just like static compiling code at the time of development, JIT is able to recompile
the code if this is found to be the most advantageous plan of action. Also, in the same vein as
static compilation, JIT is capable of enforcing security guarantees. Just like compilers,
interpreters have the capacity to translate code. Both are the primary methods of
implementing programming languages; however, the categories of ‘compiler’ or ‘interpreter’
are not distinct (for their dual roles as code translators). The most obvious disadvantage to
using an interpreter is that once the code is interpreted, the program will inevitably run slower
than when simply compiling the code; however, it takes much less time to interpret coding
than it does to compile and run it (especially pertinent when prototyping and testing code).
Generally speaking, JIT provides much better performance than interpreters, and, in many
cases, provides much better performance than static compilers. Its superiority over JIT,
however, does not bar it from having some major disadvantages: There is a slight delay when
initially executing an application (a side effect of taking time to load and compile byte code).
It will eventually generate better coding; however, the initial delay inherent in doing so will
increase with the quality of coding.
4. Define an integer variable called bank Balance. Initialize it to a value of 500. Then add
250 to it. Then subtract 100 from it. Finally, print the resulting value.
Answer:
int bankBalance = 500;
bankBalance = bankBalance + 250;
bankBalance = bankBalance - 100;
System.out.println(bankBalance);
5. Write a program that asks the user a “what-is-your-favorite” question and then displays a
nice (or a nasty) comment that incorporates the user’s answer. For example (user input is
shown in bold):
What is your favorite movie? Titanic
I think Titanic is a terrible movie!
Just kidding! I like Titanic, too.
6. Write a program that prompts the user to enter an integer and displays the entered value
times two as follows:
Enter an integer: 5
2 * 5 = 10
7. What value for register will be printed at the end of this block of Java code?
register = register + 10; //Customer pays $10. register = register - 3; //Customer receives $3
as change. System.out.println(register);
A. 19.0 D. 25.5
B. 19.5 Answer: B
C. 22.5
8. Define an integer variable called bankBalance. Initialize it to a value of 500. Then add
250 to it. Then subtract 100 from it. Finally, print the resulting value.
Answer:
System.out.println(bankBalance);
9. What value will be printed by this line of Java code? System.out.println(2.0 * (5 / 2));
A. 4 B. 4.0 C. 5 D. 5.0 E. This line of code will give an error.
Answer: B
10. Write Java code to define an integer variable called day, and a String variable called
month. Give month and day appropriate values for your birthday.
Answer:
int day;
String month;
day = 3;
month = "July";
//Note that this can be compressed into two lines by defining the
int day = 3;
11. Write the java program that take input from keyboard and calculate square root of any
integer number
12. Write Java code to create a String variable called first Name, define it to be your first
name as a String. Then define a variable called last Name and define it to be your last
name as a String. Then define a variable called full Name and set it to be your first name
followed by a space followed by your last name. Use the existing variables for your first
and last name and String concatenation to define full Name. Finally, write code to print
this text: Hello, my name is [full name]. There are [number] letters in my name. Use
String concatenation to create the first String to print using the full Name variable, and
use the. length () command on first Name and last Name to calculate the number of
letters. Note: you can concatenate an integer and a String and the integer will be
converted to a String. For example, this expression: "There are "+ 7 + "days in a week."
will be evaluated as the String: "There are 7 days in a week."
Answer:
}
14. Write a program in Java to display the multiplication table of a given integer.
TestData
Input the number (Table to be calculated): Input number of terms: 5
Expected Output:
5X0=0
5X1=5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
import java.util.Scanner;
public class Exercise14 {
{
int j,n;
System.out.println ("\n");
for(j=0;j<=n;j++)
TestData
Input number of rows (half of the diamond): 7
Expected Output:
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
16. Write a Java function named calculateTip(). The access modifier should be public, it
should have a return type of double, and it should take as input a double parameter
representing the cost of a meal at a restaurant. And finally, it should return a double equal
to 15% of the cost parameter.
Answer: