Question

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, what are the contents at ArrayList index 1 in a?

ArrayList a = new ArrayList();

a.add(7);

a.add(10);

a.add(21);

a.set(1,4);

a.remove(0);

a 10
b 7
c 4
d 21

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

3) For this question, consider the following two classes:

public abstract class C

{

    public void foo1()

   {

     System.out.println("Hello foo1");

    }

    public abstract void foo2();

    public abstract void foo3();

    public void foo1Call()

     {

        foo1();

     }

   }

public class D extends C

{

    public void foo2()

    {

        System.out.println("Hello foo2");

     }

   public void foo4()

{

       System.out.println("Hello D foo4()");

}

}

Which of the following code sequences, if any, will successfully access private method foo1 in class C?

a

C c2 = new C();

c2.foo1();

b

C c2 ;

c2 = new D();

c2.foo1();

c

D d1 = new D();

d1.foo1();

d

C c1 = new D();

c1.foo1Call();

e None of the above
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)True

Explanation: In java we cannot create object directly to a abstract class. But here what we are doing is first we ahve declared reference of class A nd then

we have created an object of class B and assigned to reference variable of class A. Class reference varaiable can can hold object of class B since A is parent of B.

So the following code correctly assigs the object reference of class B to reference of class A.

2) Answer is 21

a.add(7);

a.add(10);

a.add(21);

with these insertions arraylist will have elements in following order [7,10,21]

a.set(1,4);

now with the above statement it overwrites the value at position 1 with 4 so now list will be as follows

[7,4,21]

a.remove(0); will remove 7 from list and now list will be

[4,21]

so now at index 1 21 will be there.

3) none of the above is the right answer.

Explanation: first of all foo1() in class C is not private. And also since D is extending class C it should implement all unimplemented methods of class C. But that

doesnot happen or else class D should be abstract that was not declared. If sumed all are correct then option B and C are right answers.

Add a comment
Know the answer?
Add Answer to:
Java Programming Questions (Multiple Choice) ** Please only answer if you know it, not just copy...
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
  • 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...

  • Introduction to Java Programming Question: I’m doing a java game which user clicks on two different...

    Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically looking for codes that could make my memory match game more difficult or more interesting.(a timer, difficulty level, color, etc.) GUI must be included in the code. Here is what I got so far: package Card; import javax.swing.JButton; public class Card extends JButton{    private int id;   ...

  • 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...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • please answer in java and make possible to copy and paste. thank you!!!!! Re-implement the Rational...

    please answer in java and make possible to copy and paste. thank you!!!!! Re-implement the Rational class ****JAVA**** Extend the Number class and implement the Comparable interface to introduce new functionality to your Rational class. The class definition should look like this: public class Rational extends Number implements Comparable<Rational> { // code goes here } In addition a new private method gcd should be defined to ensure that the number is always represented in the lowest terms. Here is the...

  • Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically look...

    Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically looking for codes that could make my memory match game more difficult or more interesting.(a timer, difficulty level, color, etc.) GUI must be included in the code. Here is what I got so far: package Card; import javax.swing.JButton; public class Card extends JButton{    private int id;    private boolean...

  • i think from the very bottom part there, they just want to know what the output...

    i think from the very bottom part there, they just want to know what the output would be when you call the main() method of the Maryland class workout actually running it. 2. Consider the following code fragment taken from some package: public class Maryland extends State { Maryland() /* null constructor */ ) public void printMe() ( System.out.printin("Read it."); } public static void main(String[] args) Region mid = new State(); State md new Maryland(); Object obj new Place(); Place...

  • Can someone please help with this in JAVA? Write one application program by using the following...

    Can someone please help with this in JAVA? Write one application program by using the following requirements: 1) Exception handling 2) Inheritance a) At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods b) At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods c) At least one interface: this interface needs to have at least two abstract methods d) At least one method...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

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