Ask Computer Engineering Expert

 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.              

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92267066

Have any Question?


Related Questions in Computer Engineering

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 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