Exam 3: Arrays, Classes

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Find the output generated by the code segment. int[] array = {-1, 0, 3, -5, 5}; System.out.println(array[1]); System.out.println(array[4]);

0 5

Find the output generated by the code segment. int[] arr = {1, 2, 3, 4}; for (int j = 1; j < arr.length; j++) { arr[j] = arr[j-1] - arr[j]; } for (int j = 0; j < arr.length; j++) { System.out.println(arr[j]); }

1 -1 -4 -8

Find the output generated by the code segment. ArrayList<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add(2, "1"); list.remove(1); list.remove("1"); for (int a = 0; a < list.size(); a++) { System.out.println(list.get(a)); }

1 3 4

Find the output generated by the code segment. int[] values = {1, 3, 11, 21}; int var = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 3) == 0) { var += values[i]; } else { var++; } System.out.println(var); }

1 4 5 26

Find the output generated by the code segment. int[][] arr = {{1,2}, {3,4}}; for (int j = 0; j < arr.length; j++) { for (int k = 0; k < arr[j].length; k++) { arr[j][k] = arr[k][j] * arr[j][k]; System.out.println(arr[j][k]); } }

1 6 16 18

Find the output generated by executing the code in the main method. public class C { private int x; private int y; public C(int x, int y) { this.x = x; this.y = y; System.out.println("1st"); } public C(int x) { this(x, 1); System.out.println("2nd"); } public C() { this(1); System.out.println("3rd"); } } // End of C class public class Test{ public static void main(String[] args){ C cTri = new C(); System.out.println("---"); C cTwo = new C(1); System.out.println("---"); C cOne = new C(2, 3); } }

1st 2nd 3rd --- 1st 2nd --- 1st

Find the output generated by the code segment. int[] arr1 = {1, 2}; int[] arr2 = arr1; arr2[0] = 3; arr2 = new int[2]; arr2[1] = 4; for (int i = 0; i < arr1.length; i++) System.out.println(arr1[i]); for (int i = 0; i < arr2.length; i++) System.out.println(arr2[i]);

3 2 0 4

Find the output generated by the code segment. int[][] arr = {{0, 1}, {2, 3, 5}, {7, 8, 9, 12}, {-1, -2}}; System.out.println(arr.length); System.out.println(arr[1].length); System.out.println(arr[2][1]);

4 3 8

Which of the following statements about constructors is NOT correct? -A class can have more than one constructor. -A constructor must have the same name as the class name. -A call to a constructor must always have construction parameters. -A constructor initializes the instance variables of an object.

A call to a constructor must always have construction parameters.

Which of the following statements is true regarding classes? -Each object of a class has a separate copy of each instance variable. -Public instance variables can be accessed only by the object itself. -All objects created from a class share a single set of instance variables. -Private instance variables can be accessed by any user of the object.

Each object of a class has a separate copy of each instance variable.

This problem refers to the Student class you wrote in the previous problem. In the main method: -Create a Student array with two elements and store its reference in a variable called pupils. -Assign a Student object to the element in position 0 using the first constructor. -Assign a Student object to the element in position 1 using the second constructor. -Use the mutator method to change the name of any element. -Use the toString method to display the information for each object. public class Test {public static void main(String[] args) {//Write code here.}}

Student[] pupils= new Student [2]; pupils[0]=new Student(7654321, "RaulCool"); pupils[1]= new Student(); pupils[1].set.Name("Jane Dope"); System.out.println(pupils[0].toString();

Consider the following classes. public class D { private int x; private int y; public D(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } } public class B extends D { private int z; public B(int x, int y, int z) { super(x, y); this.z = z; } public int getSum() { return x + y + z; } } a. Describe the compiler error in class B.

The getSum method in class B refers to private instance variables defined in class D. Private instance variables defined in class D can only be used in class D. Therefore, we get a compiler error when we use variables x and y in class B.

Consider the following classes. public class D { private int x; private int y; public D(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } } public class B extends D { private int z; public B(int x, int y, int z) { super(x, y); this.z = z; } public int getSum() { return x + y + z; } } b. How can we fix the problem so that the getSum method returns the sum of the instance variables in class D and class B?

To fix the problem use the following code public int getSum(){ return getX() + getY() + z; } Note: The getX and getY methods are defined in class D. They are not defined in class B. However, class B extends class D, so class B can use any public method defined in class D.

What output is generated by the following code segment? public class Test{ public static void main(){ System.out.println("main one "); } public static void main(String[] args){ System.out.println("main two "); main(); } }

When class Test is used as the starting point of the application then the JVM initially executes the method with the header "public static void main(String[] args)". main two main one

________ is invoked to create an object. -A method with a return type. -The main -A method with the void return type. -a constructor

a constructor

What output is generated when we execute the code in the main method? public class A { private double x; public A(double x) { this.x = x; } public void displayOne() { System.out.println("class A "); } public void displayTwo() { System.out.println("data = " + x); } } // End A public class B extends A { public B(double x) { super(x); } public void displayOne() { // method overriding System.out.println("class B "); } } // End B public class Test{ public static void main(String[] args){ A a = new A(3); B b = new B(10); a.displayOne(); a.displayTwo(); b.displayOne(); b.displayTwo(); } }

class A data = 3.0 class B data = 10.0

The process of hiding object data and providing methods for data access is called ____. -initialization -abstraction -encapsulation -implementation

encapsulation

Data required for an object's use are stored in ____. -parameter variables -instance variables -methods -local variables

instance variables

Find the output generated by the following code segment. ArrayList<String> list = new ArrayList<String>(); list.add("one"); list.add("fish"); list.add(1, "two"); list.add(2, "fish"); for (int k = 0; k < list.size(); k++) { System.out.println(list.get(k)); }list.set(1, list.get(2) + list.get(3)); list.set(3, list.get(2) + list.get(1)); for (int k = 0; k < list.size(); k++) { System.out.println(list.get(k)); }

one two fish fish one fish fish fish fish fish two

Find the output generated by executing the code in the main method. public class B { private String x; public B(String x){ this.x = x; } public String getX() { return x; } public void setX(String x) { this.x = x;} } // End of class B public class Test { public static void main(String[] args){ B b1 = new B("one"); B b2 = new B("two"); B b3 =b2; System.out.println(b1.getX()); System.out.println(b2.getX()); System.out.println(b3.getX()); b3.setX(b1.getX() + b2.getX()); System.out.println(b1.getX()); System.out.println(b2.getX()); System.out.println(b3.getX()); } }

one two two one onetwo onetwo

Write a method in class Q that overrides the toString method in class P. The String returned should include the value of the instance variable defined in P and the instance variable defined in Q. public class P { private double x; public P(double x) { this.x = x; } public String toString() { return "x = " + x ; } } //end P public class Q extends P { private String y; public Q(double x, String y) { super(x); this.y = y; } }

public String toString(){ // super.toString() calls the toString() method defined in class P return super.toString() + "y = " + y ; }

Write a class called Student with the following features -Private instance variables --int studentId --String name -A constructor with the header below that initializes the instance variables. --public Student(int studentId, String name) //1st constructor -A constructor with the header below that initializes name to "John Doe" and studentId to 1234567 and uses the keyword this to call the 1st constructor. Hint: you should do this in one statement. --public Student() //2nd constructor -Accessor and mutator methods for each instance variable. -A method with the header below that returns the information stored in the instance variables as a string. For example, toString() returns "name = John Doe, studentID = 1234567" public String toString()

public class Student { private int studentID; private String name; public Student {int studentID, String name) { this.studentID=studentID; this.name=name; } public student() { this(1234567,"John Doe"); } public int getID() { return studentID; } public String getName() { return name; } public void setID(int studentID) { this.studentID=studentID; } public void setName(String name) { this.name=name; } public String toString() { return "studentID= " _studentID+ ",name= " + name; } }

Write a method with the header below that returns the average of all the values stored in the parameter called array. public static double average(double[] array)

public static double average (double[] array) { double sum=0.0; for(int i=0;i<array.length;i++) { sum+=array[i]; } return sum/array.length; ]

Write a method with the header below that returns the minimum of all the values stored in the parameter called list. public static int minimum(int[][] list)

public static int minimum(int[][]list) { int min=list[0][0]; for(int i=0;i<array.length;i++) { for(int j=0;j<array.length;j++) { if(list[i][j]<min) { min = list[i][j]; } } } return min; }

Describe the compiler error in class A and the compiler error in class B. public class A { private int x; public A(int x) { this.x = x; } public A(){ int y = 4; this(5); } } //end A public class B extends A{ private int y; public B(int x, int y) { this.y = y; super(x); } } // end B

this(5); // Error super(x); // Error If a constructor uses super or this to call a constructor then it must be the first executable statement.

Find the output generated by executing the main method. public class A { private int x; private String y; public A(int x, String y){ x = x;this.y = y; } public int getX() { return x; } public String getY() { return y; } } //End of A class public class Test{ public static void main(String[] args){ A a = new A(1, "one"); System.out.println("x = " + a.getX()); System.out.println("y = " + a.getY()); } }

x = 0 y = one

Find the output generated by executing the code in the main method. public class D { int x; public D(int x){ this.x = x; } public void displayValue(int x){ System.out.println("x = " + this.x); System.out.println("x = " + x); } } //End of D class public class Test { public static void main(String[] args) { D d = new D(1); d.displayValue(2); } }

x=1 x=2


Kaugnay na mga set ng pag-aaral

Chapter 8: Balance Sheet, content use and analysis

View Set

AGEC4400 - Agribuisiness Entrepreneurship CH 3

View Set

Assessment and Management of Patients With Vascular Disorders and Problems of Peripheral Circulation

View Set

Texas Promulgated Contract Forms: Chapters 1-3

View Set

TEXTBOOK: Ch. 1: Mass Communication Culture, and Media Literacy

View Set

COSC 118 All Chapters - Review Questions

View Set