What is the output of this code sequence

Assignment Help Computer Engineering
Reference no: EM131458326

 Part I.  Multiple Choices

1. What is the valid way to declare an integer variable named a? (Check all that apply.)

?int a;

?a int;

?integer a;

2. A constructor has the same name as the class name.

?true

?false

3. Given the following code declaring and initializing two intvariables aand bwith respective values 3 and 5,indicate whether the value of each expression is true or false.

inta = 8;

intb = 13;

Expression                          true       false

?a < b                            ____     ____

?a != b                         ____     ____

?a == 4                         ____     ____

?(b - a )<= 1       ____     ____

?b <= 5                         ____     ____

4. You can simulate a for loop with a while loop.

? true

? false

5. What are the valid ways to declare an integer array name a (check all that apply)

?int [ ] a;

?inta[ ];

? array inta;

?int array a;

6. An array a has 30 elements; what is the index of the last element?

?30

?31

?29

7. A class is analogous to a

?cookie

?blue jazz

?bakery

?cookie cutter

8. This key word causes an object to be created in memory.

?create

?new

?object

?construct

9. String is a primitive data type in Java.

? true

? false.

10. It is legal to have more than one constructor in a given class.

? true

? false

 Part II.  Reading and Understanding Code

Example: What is the output of this code sequence?

doublea = 12.5;

System.out.println(a );

11. What is the output ofthis code sequence?

String s = "Ciao";

s = s.toLowerCase();

System.out.println(s );

12.   What is the output of this code sequence?

int grade = 80;

if (grade >= 90 )

System.out.println("A");

else if (grade >= 80 )

System.out.println("B");

else if (grade >= 70 )

System.out.println("C");

else

System.out.println("D  or lower");

 13.   What are the values of i and product after this code sequence is executed?

inti = 6;

int product = 1;

do

{

product *= i;

i++;

} while ( i< 9 );

14.   What is the value of i after this code sequence is executed?

inti = 0;

for (i = 0; i< 300; i++ )

System.out.println("Good Morning!");

15.   What is the value of sumafter this code sequence is executed?

int sum  = 0;

for(inti = 10; i> 5; i-- )

sum += i;

Part III.  Fill in the code(1 point each, 4questions)

Example-1: Write the code to declare an integer variable named x  and assign x the value 777.

// your code goes here

int x  = 777;

 Example-2: This code prints the String "Hello San Diego" in all capital case.

Strings = "Hello San Diego";

// your code goes here

     System.out.println(s.toUpperCase ());

16.   This code prints the number of characters in the String "Hello World"

Strings = "Hello World";

// your code goes here

 17.   Write the code to declare a double variable named pi and assign pi the value 3.14.

// your code goes here

18.   This loop calculates the sum ofthe first five positive multiples of 3 using a whileloop (the sum will be equal to 3 + 6 + 9 + 12+15 = 45)

intsum = 0;

int countMultiplesOf3 = 0;

intcount = 1;

// your code goes here

19.   Here is a whileloop;write the equivalent forloop.

inti = 0;

while(i< 88 )

{

System.out.println("Hi there");

i++;

}

// your code goes here

Part IV. Identifying Errors in Code

(5 points: 1 point each questions)

Example: Where is the error in this code :

int  a = 3.3;

answer:ais integer type, its value can only be a whole number. 3.3 is not a whole number, it's a decimal.

20.   You coded the following on line 8 of class Test.java:

int a = 3  // line 8

When you compile, you get the following message:

Test.java:8:  ";" expected

int a = 3

^

answer:

 21.   Where is the error in this code sequence?

Strings = "Hello World";

system.out.println(s );

answer:

22.   Where is the error in this code sequence?

String s = String("Hello");

System.out.println(s );

answer:

23.   Where is the problem with this code sequence (although this code sequence does compile)?

inti = 0;

while (i< 3 )

System.out.println("Hello");

 answer:

24.   You coded the following in the class Test.java:

inti = 0;

for(inti = 0; i< 3; i++ ) // line 6

System.out.println("Hello" );

At compile time,you get the following error:

Test.java:6: i is already defined in main(java.lang.String[] )

for(inti = 0; i< 3; i++ )      // line 6

         ^

1 error

answer:

Part V.  Object Oriented Programming

Find the Error

 25.   Find the error in the following class.

public class MyClass

{

private int x;

private double y;

 public void MyClass(int a, double b)

{

x = a;

y = b;

}

}

answer:

26.  You coded the following on lines 10-12 ofclass Test.java:

Strings;                     // line 10

intl = s.length();         // line 11

System.out.println("length is "+ l ); // line 12

When you compile,you get the following message:

Test.java:11: variable s might not have been initialized.

int l = s.length( );   // line 11

^

1 error

Explain what the problem is and how to fix it.

27.   The Accountclass has two of the public methods shown as follows.

Public class Account {

// other code ...

 

     public void setName(String name){

     // code omitted

}                       // set name of the account

     public String getName(){

// code omitted;

}                       // returns the name of the account.

     // other code......

}

 

The client code is as follows:

 

public static void main(String[] args) {

        // declaring2 objects of Account class

        Account account1, account2;           

account1 = new Account("Jane Green", 500.0);

 

account2 = account1;

account2.setName("Phil Grey");

 

System.out.println("account1 owner: " + account1.getName());

System.out.println("account2owner:" +account2.getName());

      }

What is the output?

Answer:

Part VI.   Write  Short Programs

 28.  Write a program that takes two words as input from the keyboard, representing a password and the same password again. (Often, web- sites ask users to type their password twice when they register to make sure there was no typo the first time around.) Your program should do the following:

  • If both passwords match, then output "You are now registered as a new user"
  • otherwise, output "Sorry, passwords don't match"

29.

Step-A. Write a class encapsulating the concept of a student, assuming a student has the following attributes:

-       a name

-       a student ID

-       a GPA (for instance, 3.8)

Please include

-       a constructor,

-       the setter and getter methods, and

-       a public method to print out the student information.

Step-B. Write a client class (so called controlling class) to test all the methods in the above class.              

Reference no: EM131458326

Questions Cloud

Provide five security advantages of cloud-based solutions : Provide five or more security advantages of cloud-based solutions. Explain how one or more of them could have prevented the issue in the above scenario.
What is cyber warfare : What is Cyber Warfare?Definition for Cyber Warfare.Tactical and Operational Reasons for Cyber War.
What are the advantages and disadvantages of web updating : What are the advantages and disadvantages of web updating? What features would you update and how often would you perform an update? Provide a rationale.
Discuss the relevance of the article to international trade : Conduct a web search seeking a current event related to a good that has an imposed tariff or quota placed on it.
What is the output of this code sequence : What is the valid way to declare an integer variable named a?What are the valid ways to declare an integer array name a ?
Budget and collective bargaining processes : Think of a public organization with which you are familiar. Explain how it differs from a private company in terms of the following:
Compute the implied average forward rate volatility : Given the market price of the caplet is $209,801.727, and using the following inputs for the caplet (notional $100 million, strike rate k = 4 percent, maturity.
Discuss how an enterprise might be attacked : Discuss how an enterprise might be attacked and the type of security device or mechanism you would select to combat the attack.
Opener with wales corporation imprinted : Jane gives a steel letter opener with Wales Corporation imprinted on it to a foreign government official as a gift.

Reviews

Write a Review

Computer Engineering Questions & Answers

  What techniques can you use to identify improvements

What techniques can you use to identify improvements? Choose one technique and apply it to this situation- what improvements did you identify?

  Draw a message sequence diagram

Considering all the packets in the file, draw a message sequence diagram that illustrates the packets. A message sequence diagram uses vertical lines to represent events that happen at a computer over time (time is increasing as the line goes down..

  Solve the javascript program

solve the javascript program.

  What is the instruction format class and why

Explain the operation of the 'MIPS andi ' instruction. Illustrate your answer with an appropriate example. In your answer you must provide details of the andi instruction format and of what each field in that instruction format does and how an and..

  What is normalization process and the different normal forms

What are the steps for designing a relational database from a domain class model? What is the normalization process and the different normal forms? Why is normalization important?

  Manufacturers are continuously releasing firm-ware upgrades

manufacturers are continuously releasing firmware upgrades for their products. if you were the manager of a wlan how

  What are the some elements of budgets and estimates

Budgets are actually price estimates tied to detailed distribution of revenues. Dissimilar conservative monetary statements, revenue and defeat and cash flow statements.

  Algorithm for carrying out concatenation operation

Develop an algorithm that concatenates T1 and T2 into the single binary search tree. The worst case running time must be O(h), where h is the maximum of h1 and h2, the heights of T1 and T2.

  Write a test plan for the applications or web pages

In 300 words or more, write a test plan for the applications or web pages you selected as "poor" and "good". The test plan should include a statement of the scope or purpose of the test and a time table for the test.

  Object-oriented analysis

Based on the following narrative, develop either an activity diagram or a fully developed description for the use case of Add a new vehicle to an existing policy in a car insurance system.

  How many local registers are in each register window set

A RISC processor has 152 total registers, with 12 designated as global registers. The 10 register windows each have 6 input registers and 6 output registers. How many local registers are in each register window set?

  Displays several dimensions with a single plot

Use your imagination to develop your own data visualization that displays several dimensions with a single plot. (For example, color of data points can be used to categorize one dimension.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd