Question

1. What is output by the following code: ArrayList< Integer > a = new ArrayList< Integer...

1. What is output by the following code:

ArrayList< Integer > a = new ArrayList< Integer >();
ArrayList b = a;
a.add(new Integer(4));
b.add(new Integer(5));
a.add(new Integer(6));
a.add(new Integer(7));
System.out.println(b.size());

A)1

B)2

C)3

D)4

E)5

2. Assume the Student and Employee classes each extend the Person class. The Student class overrides the getMoney method in the Person class. Consider the following code:

    Person p1, p2, p3;
    int m1, m2, m3;

    p1 = new Person();
    m1 = p1.getMoney();     // assignment 1

    p2 = new Student();
    m2 = p2.getMoney();     // assignment 2

    p3 = new Employee();
    m3 = p3.getMoney();     // assignment 3

The reference to getMoney in assignment 2 is to the ________ class.

A)Child

B)Person

C)Student

D)Employee

E)this cannot be determined by examining the code

3. Assume the Student and Employee classes each extend the Person class. The Student class overrides the getMoney method in the Person class. Consider the following code:

    Person p1, p2, p3;
    int m1, m2, m3;

    p1 = new Person();
    m1 = p1.getMoney();     // assignment 1

    p2 = new Student();
    m2 = p2.getMoney();     // assignment 2

    p3 = new Employee();
    m3 = p3.getMoney();     // assignment 3

The reference to getMoney in assignment 3 is to the ________ class.

A)Child

B)Employee

C)Person

D)Student

E)this cannot be determined by examining the code

4. Consider the following code:

ArrayList< Integer > a = new ArrayList< Integer >();
int value;
a.add(4);
a.add(5);
a.add(new Integer(6));
value = a.size();
System.out.println(value);

What happens when this code is compiled?

A)A compiler error occurs on Line 1, because you cannot instantiate an ArrayList in this way.

B)A compiler error occurs on Line 3, because you cannot add an int to an ArrayList< Integer >.

C)A compiler error occurs on Line 5, because the keyword new is missing when adding an Integer.

D)A compiler error occurs on Line 6, because value must be declared as an Integer.

E)This code compiles without errors.

5. Consider the following incomplete class:

public class SomeClass
{
    public static final int VALUE1 = 30;
    public static int value2 = 10;
    private int value3 = 5;
    private double value4 = 3.14;

    public static void someMethod()
    {
        // implementation not shown
    }

    public void someOtherMethod()
    {
        // implementation not shown
    }
}

Which variable is accessible without instantiating a SomeClass object?

A)VALUE1

B)VALUE1 and value2

C)value2

D)value3

E)None of the above

6. Consider the following incomplete class:

public class SomeClass
{
    public static final int VALUE1 = 30;
    public static int value2 = 10;
    private int value3 = 5;
    private double value4 = 3.14;

    public static void someMethod()
    {
        // implementation not shown
    }

    public void someOtherMethod()
    {
        // implementation not shown
    }
}

The method someMethod is defined as static. This means

A)the method is an accessor method

B)the method is accessible outside SomeClass

C)the method is not accessible outside SomeClass

D)the method is accessible without instantiating a SomeClass object

E)the method is accessible only by using a previously instantiated SomeClass object

7. Consider the following incomplete class:

public class SomeClass
{
    public static final int VALUE1 = 30;
    public static int value2 = 10;
    private int value3 = 5;
    private double value4 = 3.14;

    public static void someMethod()
    {
        // implementation not shown
    }

    public void someOtherMethod()
    {
        // implementation not shown
    }
}

The variable VALUE1 is defined as public static final. This means

A)the variable can be changed

B)the variable is an instance variable

C)the variable is not accessible outside SomeClass

D)the variable is a constant and cannot be changed

E)the variable should not be named using all capital letters

8. Which of the following describes an overridden method?

A)One of several methods in a class with the same name but different parameter lists

B)One of several methods in a class with the same name but different return values

C)A method in a subclass with the same method heading as a method in a parent class

D)Any method that invokes super();

E)A method that contains a call to itself

9. All classes in Java are directly or indirectly subclasses of the _______ class.

A)Object

B)Reference

C)String

D)this

E)Wrapper

10. Consider the following code:

public class B1
{
     private int i;
     private int j;
     …
}

public class B2 extends B1
{
     private int m;
     private int n;
     …
}

public class B3 extends B2
{
     private int z;
     …
}

Which of the following is true regarding the use of instance variable j in class B1?

A)It is directly accessible only in B1

B)It is directly accessible only in B2

C)It is directly accessible only in B3

D)It is directly accessible in B1 and B2

E)It is directly accessible in B1, B2, and B3

11. Consider the following code:

public class B1
{
     private int i;
     private int j;
     …
}

public class B2 extends B1
{
     private int m;
     private int n;
     …
}

public class B3 extends B2
{
     private int z;
     …
}

Which of the following sets of instance variables are directly accessible in class B3?

A)i, j, m, n

B)i, j, m, n, z

C)i, j, z

D)m, n, z

E)z

12. Consider the following code:

public class A1
{
    public int x;
    private int y;
    …
}

public class A2 extends A1
{
    public int a;
    private int b;
    …
}

public class A3 extends A2
{
    private int q;
    …
}

Which of the following sets of instance variables are directly accessible in class A2?

A)a, b

B)x, a, b

C)x, y, a, b

D)x, y, a, b, q

E)y, a, b

13. Consider the following code:

public class A1
{
    public int x;
    private int y;
    …
}

public class A2 extends A1
{
    public int a;
    private int b;
    …
}

public class A3 extends A2
{
    private int q;
    …
}

Which of the following sets of instance variables are directly accessible in class A3?

A)a, b, q

B)a, q

C)x, a, b, q

D)x, a, q

E)x, y, a, b, q

14. Consider the following client code and assume that it compiles successfully.

public class MainClass
{
    public static void main(String[] args)
    {
        SomeClass myObject = new SomeClass(4, 5);

        int fred = SomeClass.SOME_VALUE;
        int barney = myObject.method1();
        int wilma = SomeClass.method2(4);
    }
}

Which of the following is a static variable?

A)method1

B)method2

C)SomeClass

D)SOME_VALUE

E)This cannot be determined by examining the above code

15. The relationship between a child (sub) class and a parent (super) class is referred to as a(n) ____ relationship.

A)abstract

B)has-a

C)is-a

D)polymorphism

E)was-a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question1

Ans) Option D(answer is 4)

Explanation:-

  • Here we declare an array list 'a' of integer.
  • Then copy array b =a.
  • So adding values in a is same in b.
  • So after insertion of values in a,its size increment to 4
  • So the copy of a is b, is also increment.
  • So b.size = 4.

----------------------------------------------------------------------------

Question 2

Ans) Option C(Student class)

Explanation:-

  • Student class is the child class of Person class.
  • In assignment 2 Person p instantiate the object of student
  • So call Student method

--------------------------------------------------------------------------------------------

Question 3

Ans) Option B(Employee class)

Explanation:-

  • Employee class is the child class of Person class.
  • In assignment 3 Person p instantiate the object of Employee
  • So call Employee method

--------------------------------------------------------------------------------------------

Question 4

Ans) Option E ( no compilation error)

Explanation:-

  • Code will work perfectly and will get output = 3.
  • Here we declare an array list as integer
  • So add directly an integer or instantiated integer value are added in the array list properly
  • So size increases to 3
  • No error happen

----------------------------------------------------------------

Question 5

Ans) Option B (VALUE1 and value2)

Explanation:-

  • In java static variables can access without instantiation of class object.

----------------------------------------------------------------------------------------------------

Question 6

Ans) Option D ( The method is accessible without instantiating a SomeClass object )

Explanation:-

  • In java static method can access without instantiation of class object.

--------------------------------------------------------------------------

Question 7

Ans) Option D ( The variable is a constant and cannot be changed )

Explanation:-

  • final is the keyword used for constants in java.
  • It cannot be changed.

----------------------------------------------------------------

Question 8

Ans) Option c( A method in a subclass with the same method heading as a method in a parent class )

Explanation:-

  • A method is said to be overriden,it has the same name and parametres and return type as the method in parent class.

------------------------------------------------------------------------------------

Question 9

Ans) Option A (Object)

Explanation:-

  • In java all classes have a parent class.
  • In classes do not have a parent class then it extends object class.

--------------------------------------------------------------

Question 10

Ans) Option A ( It is directly accessible only in B1 )

Explanation:-

  • Private variable can access the class itself.
  • Here j is the class B1 variable,so it can access the j value.

----------------------------------------------------------------------------

Question 11

Ans) Option E ( Z )

Explanation:-

  • Private variable can access the class itself.
  • Here z is the class B3 variable,so it can access the z value.

--------------------------------------------------------------------------------------------

Question 12

Ans) Option B( x,a,b )

Explanation:-

  • Private variable can access the class itself and public or protected of parent class.
  • Here a and b is in the classA2 variable and x is public variable of parent class A1, so it can access the x,a,b.

-----------------------------------------------------------------------------------------

Question 13

Ans) Option B( x,a,q )

Explanation:-

  • Private variable can access the class itself and public or protected of parent class.
  • Here x and a are in parent class public variables and q is its own variable, so it can access the x,a,q.

------------------------------------------------------------------------------

Question 14

Ans) Option d (SOME_VALUE)

Explanation:-

  • Static variables can access without instantiation of class object in java

-------------------------------------------------------------------------------------------------

Question 15

ans) Option c (is-a relation)

Explanation:-

  • In java inheritance is called "is-a" relation.
Add a comment
Know the answer?
Add Answer to:
1. What is output by the following code: ArrayList< Integer > a = new ArrayList< Integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int...

    Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...

  • Given 5. import java.util. 6. public class Sortof 7. public static void main(Stringl args) [ 8. ArrayList Integer> anew ArrayList Integer 0 9. a.add(1); а.add(5); a.add(3); 11. Collections.sort(a)...

    Given 5. import java.util. 6. public class Sortof 7. public static void main(Stringl args) [ 8. ArrayList Integer> anew ArrayList Integer 0 9. a.add(1); а.add(5); a.add(3); 11. Collections.sort(a) 12. a.add(2); 13. Collections.reverse(a) 14. System.out.printin (a) 15 16 What is the result? B. 12, 1, 3, 5] C. 12, 5, 3, 1] E. [1, 3, 5, 2 F. Compilation fails G. An exception is thrown at runtime Given 5. import java.util. 6. public class Sortof 7. public static void main(Stringl args)...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • Consider the following Java method: public static ArrayList<Integer nums ArrayList<Integer values new ArrayList<Integer0; for(int i=10; i<30;...

    Consider the following Java method: public static ArrayList<Integer nums ArrayList<Integer values new ArrayList<Integer0; for(int i=10; i<30; i-i+3) if(i%) values.add(i); return values: What is returned after the method call nums()? a. [12] b. [13] c. [14] d. [15] e. [16] O Which of the following represents the final output of the code segment below? int k: int[]A; A new int[3]: fork-0; k<A.length;k++) A[k]-A.length-k; forſk-0; k<A.length-1; k++) A[k+1]-A[k) for(int i-0;i<A.length;i++) System.out.print(A[i]+" "); a. 222 O b. 333 c. 444 d. 555 O...

  • Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...

    Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Java Programming Questions (Multiple Choice) ** Please only answer if you know it, not just copy...

    Java Programming Questions (Multiple Choice) ** Please only answer if you know it, not just copy and paste from another user. Please explain the answer. 1) Given code: public abstract class A{ } public class B extends A { } The following code sequence would correctly create an object reference of class A holding an object reference for an object of class B: A c; c = new B(); TRUE FALSE ---------------------------------------------------------------------------------------------------------------------------------------- 2) After the following code sequence is executed,...

  • Can someone please explain this piece of java code line by line as to what they...

    Can someone please explain this piece of java code line by line as to what they are doing and the purpose of each line (you can put it in the code comments). Code: import java.util.*; public class Array { private Integer[] array; // NOTE: Integer is an Object. Array() {     super();     array = new Integer[0]; } Array(Array other) {     super();     array = other.array.clone(); // NOTE: All arrays can be cloned. } void add(int value) {    ...

  • LAB: Zip code and population (generic types)

    13.5 LAB: Zip code and population (generic types)Define a class StatePair with two generic types (Type1 and Type2), a constructor, mutators, accessors, and a printInfo() method. Three ArrayLists have been pre-filled with StatePair data in main():ArrayList<StatePair> zipCodeState: Contains ZIP code/state abbreviation pairsArrayList<StatePair> abbrevState: Contains state abbreviation/state name pairsArrayList<StatePair> statePopulation: Contains state name/population pairsComplete main() to use an input ZIP code to retrieve the correct state abbreviation from the ArrayList zipCodeState. Then use the state abbreviation to retrieve the state name from the ArrayList abbrevState. Lastly,...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {   super();   System.out.println(“B() called”); } public...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT