0% found this document useful (0 votes)
74 views8 pages

Albert2, Class, Method - Docx 2887

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views8 pages

Albert2, Class, Method - Docx 2887

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1. Consider the following class Shoe.

There are several types of shoes; casual, dress, sports, running, and
snow.

public class Shoe{


int shoe_type;
String brandName;
int price;
int color;

//other methods and attributes are not included


}
Which one of the following best describes the error in the above code?

A-The attributes shoe_type and color must be declared String type and price must be declared double.
B-The attribute brandName must be of the type int. Other attributes are fine.
C-The attribute price has to be of type double. Other attributes are fine.
D-The attribute shoe_type has to be of type String. Other attributes are fine.
E-The attribute brandName has to be of type int. Other attributes are fine.

2. Create a class named 'Student' with String variable 'name' and integer variable 'roll_no'. Create a new
object in driver class. Assign the value of roll_no as 2 and that of name as "John" by creating an object
of the class Student.
ss Student{
3. Consider the following class.
public class CrazyArithmetic{
String answer;

public CrazyArithmetic(int a, int b, String operator){



}

public CrazyArithmetic(double a, double b, String operator) {



}

public String getAnswer(){


return answer;
}
}
Which of the following statements would execute without throwing an error?
I. CrazyArithmetic c1 = new CrazyArithmetic(5.0, 10.0, add);
II. CrazyArithmetic c2 = new CrazyArithmetic(20.0, 8, "/");
III. CrazyArithmetic c3 = new CrazyArithmetic(4, "/");

4. Consider the following Customer class.


public class Customer
{
int custNum;
String firstName;
String lastName;
String homePhone = "";
double totPurchaseAmt = 0;

public Customer(int cNum, String fName, String lName, String hPhone, double
totPurch)
{
custNum = cNum;
firstName = fName;
lastName = lName;
homePhone = hPhone;
totPurchaseAmt = totPurch;
}
public Customer(int cNum, String fName, String lName, double totPurch)
{
custNum = cNum;
firstName = fName;
lastName = lName;
totPurchaseAmt = totPurch;
}

public Customer(int cNum, String fName, String lName)


{
custNum = cNum;
firstName = fName;
lastName = lName;
}
// other data and methods not shown
}

Which of the following statements will result in a syntax error?


A-Customer greg = new Customer(56246, "Greg", "Johnson");
B-Customer valerie = new Customer(300, "Valerie Sims", "436-945-7020");
C-Customer betty = new Customer(200, "Betty", "Lewis", "446-463-9515");
D-Customer gene = new Customer(199, "Gene", "Shelton", 200.00);
E-Customer joe = new Customer(100, "Joe", "King", "346-238-9435", 200.00);

5.
public class Sport {
public void playGames() {
// code is not included
}
public static void main(String args[] ){
Sport game= null;
game.playGames();
System.out.println(" I played games");
}
}
Which of the following statements are true for the above code?
A-The code will print out: I played games.
B-The code will throw NullPointerException.
C-The code will run without error.
D-The null reference to call the method playGames() causes an error.
E-The variable game can't point to a null value.

6. Consider the following class declaration Student used for an Art Program.
public class Student {
int studentID;
String firstName;
String lastName;
String course1;
String course2;
String course3;

public Student ( int ID, String f_name, String l_name, String cs1, String cs2 ,
String cs3) {
studentID=ID;
firstName =f_name;;
lastName= l_name;
course1=cs1;
course2=cs2;
course3=cs3;
}

public Student ( int ID, String f_name, String l_name, String cs1) {
studentID=ID;
firstName =f_name;;
lastName= l_name;
course1=cs1;
}

public Student ( int ID, String f_name, String l_name, String cs1, String cs2) {
studentID=ID;
firstName =f_name;
lastName= l_name;
course1=cs1;
course2=cs2;
}
//code for other methods and variables not included
}
Which of the following constructors would create the following objects?
Amy, who registered for Painting, Drawing, and Sculpture.
Anish, who registered for Drawing.

(I)
Student Amy =new Student ( "1", "Amy", "Welly", "Drawing", "Painting", "Sculpture");
Student Anish =new Student ( "23", "Anish", "Shah", "Drawing");
(II)
Student Amy =new Student ( 1,"Amy", "Welly", "Drawing", "Painting", "Sculpture");
Student Anish =new Student ( 23,"Anish","Shah", "Drawing");
(III)
Student Amy =new Student ( "1", "Amy", "Drawing", "Painting", "Sculpture");
Student Anish =new Student ( "23", "anish", "shah", "Drawing", "Painting");
(IV)
Student Amy =new Student ( 1,"Welly", "Drawing", "Painting", "Sculpture");
Student Anish =new Student ( 23,"Shah", "Drawing");

7. Consider the following class declaration.


public class Desk {
int height;
int width;
int length;
String color;
boolean isFolding;
String type;

public Desk (){


height=1;
width=1;
length=1;
color="default";
isFolding=False;
type= "default";
}
public Desk (int h, int w, int l, String col){
height=h;
width=w;
length=l;
color=col;
}
// Other methods and code are not included
}
Which of the following can be used to create an object mydesk of the type Desk?
I. mydesk= new Desk(12,15,12, "red");
II. mydesk= new Desk(34,15,22, "pink", True);
III. mydesk= new Desk();
IV. mydesk=Desk.new();

8. Consider the following code to create a pet object.


public class Pet {
String type;
String breed;
int age;
public Pet (){
}
public Pet (String ptype, String pbreed , int years){
type=ptype;
breed=pbreed;
age=years;
}
public static void main( String args[]) {
//missing code
Pet bD=smallDog;
Pet myDog = new Pet("watch dog","poodle",2);
System.out.println("Type:"+myDog.type+" Breed "+bD.breed+"Age "+ bD.age);
}
}
The output of this code is
Type: watch dog Breed null Age 0
Which of the following would best describe the missing code?
I. Pet smallDog=new Pet("watch dog", "poodle",2);
II. Pet smallDog=new Pet();
III.String smallDog= new String ("poodle");
IV. Pet smallDog;
smallDog=new Pet();

9. Consider the following two class definitions.


public class GameBoard {
int length=0;
int width=0;
public GameBoard (int l, int w){
length=l;
width=w;
}
}

public class Game {


GameBoard board;
String player1;
String player2;
public Game ( GameBoard board, String X String Y){
this.board=board;
player1=X;
player2=Y;
}
public Game ( String X String Y){
player1=X;
player2=Y;
}
// other methods and code are not included.
}
Determine which code segment can be used to create Game object compete on a board object of
type GameBoard of size 15 x 15 pixels for the player objects Ann and Jay.
I.
GameBoard board = new GameBoard (15,15);
Game compete= new Game (Board, "Ann","Jay");
II.
GameBoard board = new GameBoard ();
Game compete= new Game (board, "Ann","Jay");
III.
GameBoard board = new GameBoard (15,15);
Game compete= new Game ("Ann","Jay");
IV.
GameBoard board = new GameBoard (15,15);
Game compete= new Game (board, "Ann","Jay");
10. Consider the following code used by a candy store owner. The store carries 4 x 4 birthday-special
chocolate bars. The store creates custom chocolate bars for special occasions.
public class ChocolateBar {
int length;
int width;

public ChocolateBar (){


length=4;
width=4;
}
public ChocolateBar( int l, int w){
length =l;
width=w;
}
// other methods and code are not included
}
Which if the following constructor calls best describes a way to create objects called small and big of the
type ChocolateBar of the size 4 x 4 and 8 x 8 respectively?

A
ChocolateBar small = new ChocolateBar();
ChocolateBar big =new ChocolateBar(8,8);

B
ChocolateBar small = ChocolateBar();
ChocolateBar big = ChocolateBar(8,8);

C
ChocolateBar small = New chocolateBar();
ChocolateBar big =New chocolateBar(4,4);

D
ChocolateBar small = new ChocolateBar(4,4);
ChocolateBar big =new ChocolateBar();

E
ChocolateBar small = new ChocolateBar();
ChocolateBar big =new ChocolateBar(8);

11. Consider the following code.


public class Employee{
String first;
String last;

public Employee (String fname, String lname){


first=fname;
last=lname;
}
public Employee(){
}

public static void main (String args[]){


// missing code
System.out.println("Employee no.1 is:"+ Emp1.first+ " "+ Emp1.last);
System.out.println("Employee no.2 is:"+ Emp2.first+ " "+ Emp2.last);
System.out.println("Employee no.3 is:"+ Emp3.first+ " "+ Emp3.last);
}
}
The following is the output of this code:
Employee no. 1 is: James Green
Employee no. 2 is: Sheen Blue
Employee no. 3 is: null null

Which of the following answers best describe the code that can replace the missing code?
I.
Employee emp1 = new Employee ("James" , "Green");
Employee emp2 = new Employee ("Sheen" , "Blue");
Employee emp3 = new Employee ("null","null");
II.
Employee emp1 = new Employee ("James" , "Green");
Employee emp2 = new Employee ("Sheen" , "Blue");
Employee emp3 = new Employee (null, null);
III.
Employee emp1 = new Employee ("James" , "Green");
Employee emp2 = new Employee ("Sheen" , "Blue");
Employee emp3 = new Employee ();
IV.
Employee emp1 = new Employee ("James" , "Green");
Employee emp2 = new Employee ("Sheen" , "Blue");
Employee emp3 = New Employee ();
V.
Employee emp1 = new Employee ("James" , "Green");
Employee emp2 = new Employee ("Blue " , " Sheen ");
Employee emp3 = new Employee ();

12. Consider the following code.


public class SnackBox {
String content;
int things;
public SnackBox(){
}
public SnackBox(int count ){
things=count;
}
public SnackBox(String snack , int count){
content=snack;
things=count;
}
}
Which one of the following best describes code for creating the following three SnackBox objects?
An empty object nosnack.
A fewthings object with 5 things in it.
A cookiebox object with eight cookies.

A
SnackBox nosnack = new SnackBox();
SnackBox fewthings = new SnackBox(5);
SnackBox cookiebox = new SnackBox("cookies", 8);

B
SnackBox nosnack = new SnackBox();
SnackBox fewthings = new SnackBox("cookies", 8);
SnackBox cookiebox = new SnackBox(5);

C
SnackBox nosnack = new SnackBox("cookies", 8);
SnackBox fewthings = new SnackBox(5);
SnackBox cookiebox = new SnackBox();

D
SnacKBox nosnack = new Snackbox();
SnaCkBox fewthings = new Snackbox(5);
SnackBox cookiebox = new Snackbox("cookies", 8);
E
SnackBox nosnack = SnackBox();
SnackBox fewthings = SnackBox(5);
SnackBox cookiebox = SnackBox("cookies", 8);
13. Consider the class below.
public class Example{
public int number;
public Example(int n){
number = n;
}
public int getNumber(){
return number;
}
public void setNumber(int n){
number = n;
}
}
In a client class, which of the following will display the number 7?

A
Example e = new Example(5);
System.out.println(e.getNumber(7));

B
Example e = new Example(5);
System.out.println(e.setNumber(7));

C
Example e = new Example(7);
e.getNumber();

D
Example e = new Example(5);
e.setNumber(7);
System.out.println(e.getNumber());

E
Example e = new Example();
e.setNumber(7);
System.out.println(e.getNumber());

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy