0% found this document useful (0 votes)
17 views7 pages

Exam 3 Review Solutions

Uploaded by

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

Exam 3 Review Solutions

Uploaded by

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

COSC 1436, Fall 2024

Exam 3 Review

1. List the 4 main principles of Object Oriented programming.

Abstraction, Encapsulation, Inheritance, Polymorphism

2. When sending multiple inputs to a method, which of the following are true?
a. The order of the inputs and the types must match, but the names in the actual
and formal parameters don’t matter.
b. The order of the inputs must match, but the types and the names in the actual
and formal parameters don’t matter.
c. The order of the inputs and the types don’t matter, but the names in the actual
and formal parameters must match.

3. Given the following header line to a method, which of the following correctly calls the
method?
public static void Hello(int a, String b);

a. Hello("Hi", 4);

b. int answer=0;
answer=Hello(3,4);

c. int value=3;
Hello(value,4);

d. Hello(4,"Hi");

e. int answer=0;
answer= Hello(4,"Hi");

4. What the output of the following program fragment?

public static void main(String[] args){


int a=4;
int b=7;

method1(b,a);
System.out.print(" main a= " +a);
System.out.print(" main b= " + b);
}

Page 1 of 7
public static void method1(int a, int b){
System.out.print("method a= " +a);
System.out.print(" method b= " + b);
}

a. method a=7 method b=4 main a=4 main b=7


b. method a=4 method b=7 main a=4 main b=7
c. method a=4 method b=7 main a=7 main b=4
d. method a=7 method b=4 main a=7 main b=4

5. What the output of the following program fragment?

public static void main(String[] args){


int c=4;

System.out.print(" main c= " +c);


method1(c);
System.out.print(" main c= " + c);
}

public static void method1(int c){

c=9;
System.out.print("method c= " +c);
}

a. main c=4 method c=9 main c=9


b. main c=4 method c=4 main c=4
c. main c=9 method c=9 main c=9
d. main c=4 method c=9 main c=4

6. Which of the following can be declared to be private in a class?


• Fields
• Methods

7. When something is declared private in a class, what methods allowed to access it?
a. Any method anywhere?
b. No method
c. Only methods listed on an access list on the first line of the method
d. Only other methods in the same class

8. An instance is a “blueprint” or “template” for a class. True or False?

9. In a UML diagram of a class, there are three sections. From top to bottom, the sections
are:

Page 2 of 7
a. Name of class, methods in the class, fields in the class
b. methods in the class, fields in the class, name of class
c. Name of class, fields in the class, methods in the class
d. methods in the class, name of class, , fields in the class

10. In a UML diagram of a class, the “+” and “-“ stand for:
a. “+” is a field, “-“ is a method
b. “+” is an instance, “-“ is an object
c. “+” is public, “-“ is private
d. “+” has a return value, “-“ is void

11. Which of the following is/are true of constructors


I. If you do not create your own constructor, Java will provide a “default
constructor” that uses default values for all of the fields
II. If you have multiple constructors, each of them have to have the same inputs
III. If you have multiple constructors, each of them have to have the same name

a. I only
b. II only
c. I and III only
d. II and III only
e. I, II, and III

12. What is the primary benefit of ArrayList over arrays?


a. You can make an ArrayList of char and you cannot make an array of char
b. Arraylists don’t need to import any extra libraries, but arrays do
c. Arraylists can change size during the program, and arrays have to stay the same
size
d. Arraylists use less memory

13. What is the primary disadvantage of ArrayList compared to arrays?


a. Arrays can be used in loops, and ArrayList cannot
b. You need a loop to print out all of the elements of an ArrayList
c. You cannot delete elements from ArrayList while the program is running
d. Arraylists run slower

14. Which of the following correctly declares an ArrayList of ints?


a. ArrayList int datalist = new ArrayList();
b. ArrayList<Integer> datalist = new ArrayList<Integer>();
c. ArrayList<Int> datalist = new ArrayList<Int>();
d. ArrayList[Integer] datalist = new ArrayList[Integer];
e. ArrayList <Integer> datalist = new ArrayList <Integer>();

Page 3 of 7
15. Draw a UML diagram for a class with the following characteristics:
• Class name = Car
• Fields (all private): Car
o String make=”Honda”
o int year=2024 -make:String= ”Honda”
o String color=”white” -year:int = 2024
o String vin= -color:String=”white”
-vin:String
• Methods:
o public void setMake (String newmake)
o public String getMake( ) +setMake(newmake: String):void
o public String getVIN( ) +getMake( ):String
o private void setVIN (String newVIN) +getVIN( ):String
-setVIN(newvin: String):void

16. Write the code to implement the class in the previous question. Add in the following
constructors
a. A public constructor that lets you set the make
b. A public constructor that lets you set the make and the vin
public class Car{
private String make="Honda";
private int year=2024;
private String color="white";
private String vin;

public Car (String newmake){


vin=newmake;
}

public Car (String newmake, String newvin){


make=newmake;
vin=newvin;
}

public void setMake(String newmake){


make=newmake;
}

public String getMake(){


return make;
}

public String getVIN(){


return vin;
}

private void setVIN(String newvin){


vin=newvin;
Page 4 of 7
}
}
17. Write a whole main program to create the following two instances of the car class. Also,
print out the makes of each car using the class’s methods.
a. Instance name = car1
• Make=”Ford”
b. Instance name = car2
• Make=”VW”
• vin=”A1B2”

public class Exam3reviewQ17{


public static void main(String[] args){
Car car1=new Car("Ford");
Car car2=new Car("VW","A1B2");
System.out.println(car1.getMake());
System.out.println(car2.getMake());
}
}
OR

public class Exam3reviewQ17{


public static void main(String[] args){
Car car1=new Car();
Car car2=new Car("VW,"A1B2"); //since setVin is private, you can't
//use it in the main program, and
//have to use the constructor. car1
//only need to set the make, and
//that is public
car1.setMake("Ford");
System.out.println(car1.getMake());
System.out.println(car2.getMake());
}
}

18. Write a method that converts inches to cm. It must take a double as the number of
inches as an input and outputs the number of cm back to the main program. You can
assume that the main program will print out the answer to the screen. Remember that
1 in=2.54 cm.

public static double inchestocm(double inches){


double cm=0;
cm=inches*2.54;
return cm;
}

Page 5 of 7
19. Write another method that converts inches to cm. It must take a double as the number
of inches as an input. But instead of outputting the number of cm to the main
program, the method prints the answer to the screen. Remember that 1 in=2.54 cm.

public static void inchestocm(double inches){


double cm=0;
cm=inches*2.54;
System.out.println("cm= "+cm);
}

20. Write a method that prompts the user for an integer, reads the integer from the
keyboard, and sends it back to the main program.
public static int getInt(){
Scanner keyboard = new Scanner(System.in);
int input=0;

System.out.println("What's your number?");


input=keyboard.nextInt();
return input;
}

21. Write a whole program that asks the user for a measurement in inches and uses a
method to convert it to cm. You can use your method from #18 as part of it.
import java.util.Scanner;

public class Exam3Review17{


public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double inches=0;
double cm=0;

System.out.println("How many inches?");


inches=keyboard.nextInt();
cm=inchestocm(inches);
System.out.println("cm= "+cm);
}
public static double inchestocm(double inches){
double cm=0;
cm=inches*2.54;
return cm;
}

Page 6 of 7
22. Write a whole program that declares an ArrayList of integers and does the following
operations:
Add the value 6
Add the value 9
Add the value 14
Print out the value of just the last element
Insert the value 7 in between the 6 and 9
Delete the 9
Print out the size of the ArrayList
Print out the whole ArrayList

import java.util.ArrayList;
public class arraylisttest{
public static void main(String[ ] args){
ArrayList<Integer> data = new ArrayList<Integer>();

data.add(6);
data.add(9);
data.add(14);
System.out.println(data.get(2));
data.add(1,7);
data.remove(2);
System.out.println("Size is:" + data.size());
System.out.println(data);
}
}

Page 7 of 7

You might also like