Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

PART - A

1.  To compile and run a Java source file "Hello.java" that contains a main() method which of the following are the correct commands?

A.  to compile use "javac Hello.java" then to execute use "java Hello"
B.  to compile use "javac Hello" then to execute use "java Hello.class"
C.  to compile use "javac Hello.java" then to execute use "java hello"
D.  to compile use "javac Hello.java" then to execute use "java Hello.class"  
 
2.  Which of the following Java code fragments compile correctly?

A.  String x = 2;         
B.  String s = "Hello";    
C.  String myName;    myname = "Peter";  
D.  String s;    S = "George";
 
3.  A Java virtual machine runs

A.  The commands in the .java files
B.  The bytecode in the .class files
C.  Does not run any code
D.  It compiles the commands written in the .java files
 
4.  Constructor methods must be defined to have what return type?

A.  int
B.  void
C.  the return type should be the same as the class
D.  constructors do not have return types

5.  Assuming that the following code fragment is part of a class, what will the concatenate method return if passed the string "Mary"?

String s = "Peter";
String concatentate(String s)
{
return s + this.s + s;
}
 
A.  Mary Mary Mary
B.  Peter Peter Peter
C.  Mary Peter Mary
D.  Peter Mary Peter

6.  What is the most likely method signature of the "parseInt()" if it can be called as follows: int c = Integer.parseInt("34");

A.  public void parseInt(String number)
B.  public static void parseInt(String number)
C.  public int parseInt(String number)
D.  public static int parseInt(String number)

7.  In Java, which of the following is the correct way to create a new object of the class MP3?

A.  MP3 downloadSong = new MP3;  
B.  MP3 *downloadSong = new MP3();
C.  MP3 downloadSong = new MP3();
D.  None of the above

8.  What would be the value of variable c after executing the following statement: int c = Integer.parseInt("a");

A.  a
B.  97 which is ASCII value of a  
C.  The statement would throw a NumberFormatException
D.  0
E.  "a"

PART B:
B1.
 
(a) Consider the code below:     
public class ArgsExample {
  public static void main(String[] args) {
    String from = "";
    String to = "";
    String subject = "";
    String emailText = "";
    for (int i = 0; i < args.length; i++) {
      if (args[i].startsWith("From:")) {
        from = args[i];
        System.out.println("MAIL FROM: " + from);
      } else if(args[i].startsWith("To:")) {
        to = args[i];
        System.out.println("RCPT TO: " + to);
      } else if(args[i].startsWith("Subject:")) {
        subject = args[i];
        System.out.println("SUBJECT: " + subject);
      } else if (args[i].startsWith("Body:")) { //The rest
        for (int j = i; j < args.length; j++)
        {
          emailText += args[j];
        }
      System.out.println(emailText);
      System.out.println(".");
      }
    }// for
  }//main
}

Understand the code and observer  the output after running this program with command-line arguments as follows

    > java ArgsExample To:xyz@abc.edu.au From:abc@abc.edu.au Subject:Hello Body:This is message
 
Look at the code above and run it with above command-line arguments, answer the following two questions:

i.  What is stored in args?

ii.  What is stored in the variable emailText just after the statement

(emailText += args[j];// where j=4) is executed?

(b)

Modify the program above to make the output looks more clear by removing "To:", "From:", "Subject:",   "Body:" and add space for "Thisismessage" (i.e. make it become "This is message"). Also, let MAIL FROM come before RCPT TO. Run your program using the command above. Note that To:xyz@abc.edu.au must be the first command-line argument. The program should output to the screen as follows:

  number of args is: ###
MAIL FROM: abc@abc.edu.au
  RCPT TO: xyz@abc.edu.au
  Subject: Hello
  This is message

Where ### represents actual number of commend-line arguments. Remember to output the correct value.

We will use the command above in a command window to run your program. We will also use different email addresses, subject, email message, to run your program.

Your program should output to the screen as follows:
 
  number of args is: ###
MAIL FROM: 1234567@abc.edu.au
  RCPT TO: xyz@gmail
  Subject: Meeting
  See you at 1pm

It is important that you run your program with the commands above, and check its execution and output before submitting it to ESP.

B2.

Random number generators are frequently used in many applications. In Java you can use method random() from library Math, for example:

double randNum = Math.random();

This statement will return a random number between 0.0 and 1.0 (excluding 1.0). If you need an integer number you have to convert rundNum into int,    for example if you want to generate an integer number 1, 2, ..., 6 you may do in that way:

int i = 1 + (int) ( Math.random() * 6);
 
Write a program that simulate n rolls of six-sided die and displays the frequency of occurrence of each side. For example, If n = 6000, then each side is expected to occur about 1000 times. A sample output could be as follows:

Face 1 frequency= 999
Face 2 frequency= 1031
Face 3 frequency= 1010
Face 4 frequency= 1000
Face 5 frequency= 1026
Face 6 frequency= 934
 
You are required to use the following command line to execute your program in DOS (Windows Console, i.e. cmd.exe).

-  java RollDie6 6000
 
If you use any IDE to create this program, it is important that you run it with our command in DOS, and check its execution and output before submitting it to ESP. We will use different numbers for rolls to test your program, e.g.

-  java RollDie6 12000
 
For example, an output format could be as follows:

-  java RollDie6 6000

Face 1 frequency= 1037
Face 2 frequency= 990
Face 3 frequency= 973
Face 4 frequency= 968
Face 5 frequency= 988
Face 6 frequency= 1044
 
Hints: you can use a for loop to simulate n (e.g. n=6000) rolls of six-sided die, i.e.

for ( int roll = 1; roll <= n; roll++ )

{ /*each time you get an integer number between 1 and 6 generated randomly. e.g. if this number is 3, the frequency of occurrence of the side Face 3 is increased by 1. */ .............

Java, Programming

  • Category:- Java
  • Reference No.:- M9741581

Have any Question?


Related Questions in Java

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Can someone help me please with those question1what is the

Can someone help me please with those question 1:what is the best data type for student id datatime,currency,number,decimal 2:which relationshipis preferable? one to one,one to many,many to many 3:if you add table A's pr ...

Can someone please help me with the following java

can someone please help me with the following java question The input is an N by N matrix of nonnegative integers. Each individual row is a decreasing sequence from left to right. Each individual column is a decreasing s ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

Solving 2nd degree equationsbull write the following java

Solving 2nd degree equations • Write the following Java methods • boolean real-sols(double a, double b, double c): it returns true if the 2nd degree equation ax2 + bx + c has real solutions • double solution1(double a, d ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As