Student Mark Islamic University of Gaza Faculty of Engineering Computer Engineering Department Question # 1 / 18 Question # / 1 Total ( 0 ) Student Information ID Name Answer keys Sector A B C D E A B C D E F G H I J K L M Question Answers Table
A What is the printout of the following code public static void main(string [] args) { A p = new A(); System.out.println(p); Class A0 //System.exit(0); Class A0 System.out.println(p); int i; A() { String tostring() { return Class A + i; B What is the printout of the following code A p = new A(); A q = new A(); System.out.println(p.f()); System.out.println(q.f()); p = q = new A(); System.out.println(p.f()); System.out.println(q.f()); System.out.println(new A().f()); System.out.println(A.y); int x; static int y; A() { x = y; y++; int f() { return x; C What is the printout of the following code public public static void main(string[ ] args) { A a = new A( Welcome to Java ); a.print(); Welcome to Java String s; A(String s) { this.s = s; void print() { System.out.println(s); D What is the printout of the following code public static void main(string [] args){ A a = new A(); System.out.println(a.i + +a.f()); A a1 = new B(); System.out.println(a1.i+ +a1.f()); A a = (B)a1; System.out.println(a.i+ +a.f()); class B extend A{ int i = 8; int f(){ return ; int i = 9; int f(){ return 5; 9 5 9 9 0 1 4 E What is the printout of the following code { int p = 1, q = ; p = q; p = ; System.out.println(p); System.out.println(q); { A p = new A(1), q = new A(); p = q; p.f(); System.out.println(p.x); { int p = 4, q = 4; System.out.println(p == q); { A p = new A(4), q = new A(4); System.out.println(p == q); int x; A(int p) { f(p) ; void f(int p) { x = p; F What is the printout of the following code int p; A q; p=1; e(p); System.out.println(p); p=; f(p); System.out.println(p); p=; p=f(p); System.out.println(p); q = new A(); q.x = 4; g(q); q = new A(); q.x = 5; h(q); q = new A(); q.x = 6; q = h(q); static void e(int p) { p = 7; static int f(int p) { p = 8; return p; static void g(a p) { p = new A(); static A h(a p) { p.x = 10; return p; int x; true false 1 8 4 10 10 G What is the printout of the following code public public static void main(string[ ] args) { String s1 = Welcome to Java! ; String s = s1; if (s1 == s) System.out.println( s1 and s reference to the same String object ); else System.out.println( s1 and s reference to different String objects ); s1 and s reference to the same String object
H What is the printout of the following code A p = new A(new A(1),, new A(, new A(4))); p.z.f(5); p.f(6); System.out.println(A.g(p)); System.out.println(A.g(p.y)); System.out.println(A.g(p.z)); System.out.println(A.g(p.y.y)); System.out.println(A.g(p.z.z)); System.out.println(A.g(p.y.z)); int x; A y, z; A(int p) { x = p; A(int p, A q) { x = p; z = q; z.y = this; A(A p, int q, A r) { this(q, r); y = p; void f(int p) { new A(this, p, z); static int g(a p) { if (p == null) return 0; return p.x; 1 0 4 0 I What is the printout of the following code public public static void main (String args[]){ //System.out.println(new A()); A.print(); class A private A() { static void print(){ System.out.println( class A ); K What is the printout of the following code public public static void main( String args[] ) { C c = new C(); System.out.println(c.max(1,9)); class C extends B { 9 int max(int x, int y) { return super.max(x+10, y+10); class B extends A { int max(int x,int y) { return super.max(x,y) 10; int max(int x,int y) { if (x>y) return x; else return y; L What is the printout of the following code public A aa = new A("Test"); System.out.println(aa.f()); test System.out.println(aa); Test final private final String s; public A(String str) { this.s = str; String f() { return s.tolowercase(); public String tostring() { return this.s; J What is the printout of the following code public public static void main (String[] args) { StringBuffer a = new StringBuffer( Hello ); System.out.println(a.capacity()); a.ensurecapacity(0); 1 System.out.println(a.capacity()); 44 a.ensurecapacity(55); 90 System.out.println(a.capacity());
Question # : Choose the correct answer A.. represents an entity in the real world that can be distinctly identified. a) An object b) A data field c) An method d) A Class B Given the declaration Circle x = new Circle(), which of the following statement is most accurate? a) x contains an object of the Circle type. b) You can assign an int value to x. c) x contains an int value. d) x contains a reference to a Circle object. C Which of the following statements is most accurate? (Choose all that apply.) a) A reference variable is an object. b) An object may contain the references of other objects. c) An object may contain other objects. d) A reference variable refers to an object. D Variables that are shared by every instances of a class are.. a) public variables b) class variables c) instance variables d) private variables E To prevent a class from being instantiated,.. a) use the private modifier on the constructor. b) don't use any modifiers on the constructor. c) use the public modifier on the constructor. d) use the static modifier on the constructor. F Suppose s1 and s are two strings. Which of the following statements or expressions is incorrect? (Choose all that apply.) a) String s = s1 - s; b) char c = s1[0]; c) boolean b = s1.compareto(s); d) char c = s1.charat(s1.length()); G Assume s is "ABCABC", the method.. returns a new string "abcabc". a) s.replace('a', 'A') b) s.tolowercase() c) s.replace("abcabc", "abcabc") d) s.replace('a', 'a') H Which class do you use to read data into a text file? a) Scanner b) PrintWriter c) System d) File I Object-oriented programming allows you to derive new classes from existing classes. This is called.. a) abstraction b) encapsulation c) inheritance d) generalization J Which of the following statements are true? (Choose all that apply.) a) "Class A extends B" means B is a subclass of A. b) A subclass is usually extended to contain more functions and more detailed information than its superclass. c) "Class A extends B" means A is a subclass of B. d) A subclass is a subset of a superclass. K Polymorphism means.. a) that a variable of supertype can refer to a subtype b) that data fields should be declared private object c) that a class can extend another class d) that a class can contain another class
L Which of the following statements are true? (Choose all that apply.) a) Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them. c) It is a compilation error if two methods differ only in return type in the same class. M e) A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden. Which of the following statements are true? (Choose all that apply.) b) A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated. d) To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass. a) The compiler finds a matching method according b) Dynamic binding can apply to instance methods. to parameter type, number of parameters, and order of the parameters at compilation time. c) Dynamic binding can apply to static methods. d) You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as polymorphism. e) A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime. Good Luck
Practical Question Write a class named Book that keeps track of book objects such that the instance data contains the book s title and author and a unique identification number, say id that starts form 1 for the first book object and is incremented by 1 each time a new book with at least the title specified is created. The required methods in the Book class are as follows: constructors: One without parameters that sets the title and author to unknown and id to 0; one with a single parameter that sets the title to the given value as parameter, sets the author to unknown, increments the sequence by 1 and sets id to this sequence number; one with two parameters for title and author, setting the corresponding instance data, and id as in the second constructor. The setter methods for title and author. The getter methods for title, author, and id. The equals method that compares the current book object with another Book object given as a parameter and returns true if both objects have the same title and author properties, and false otherwise. The tostring method that returns a text including the book s title, author and id. Refer to the sample execution window for the test program for details. The getinitials method that returns the initial letters of the author s first name(s) and last name, if the author s name is known (not equal to unknown, or not null).assume that there can be at most two names and one surname separated by a single blank. The search method that return information based on book id; Write a driver class that tests the Book class. Allow the user to enter as many book objects as s/he wants. Take care of necessary object initializations. Store the first and last book objects separately. Print each book object. Compare the first and last book objects, if any, and display a proper message if they are the same. Display the last book s author s initials, if the user has input any valid book object. Test search method.