Quiz34 Classes Objects Nested Classes PDF

Title Quiz34 Classes Objects Nested Classes
Author Nguyen Ngoc Man (K15 DN)
Course Object-Oriented Programming
Institution FPT University
Pages 14
File Size 277.3 KB
File Type PDF
Total Downloads 74
Total Views 133

Summary

Download Quiz34 Classes Objects Nested Classes PDF


Description

1) Which of the following is a valid declaration of an object of class Box? a. Box obj = new Box(); b. Box obj = new Box c. obj = new Box(); d. new Box obj 2) A_____ are methods which are in the same class and have the same name but different parameter lists. a. Method overloading b. Method overriding c. Method sharing d. Method hiding 3) How do you create an instance of Box's Filler class? class Box{ static class Filler{ int min, pref, max; public Filler(int min, int pref, int max){ this.min = min; this.pref = pref; this.max = max; } } }

4)

5)

6)

7)

a. new Box().new Filler(1,2,3); b. Filler filler = new Box().new Filler(1,2,3); c. Box.Filler filler = new Box.Filler(1,2,3); d. Box.Filler filler = new Filler(1,2,3); The ______ enable you to logically group classes that are only used in one place? a. nested class b. anonymous class c. local class d. final class Select FALSE statement about static method in Java a. Can access non-static and static variables both b. Belongs to the class and not its instances c. Can be called without any object creation of a class d. Can access static variables We can apply Java static keyword with variables, methods, _______ and nested class. a. blocks b. outter classes c. constructors d. packages What is an immutable object? a. The one with no methods to modify its fields b. The one has setter methods c. The one has setter and getter methods

d. The one can be modified its fields 8) What is the output of following code? class Example { private int a, b; public Example(int a, int b) { this.a = a; this.b = b; } public void display(int a) { System.out.println(a*b); } public static void main(String args[]) { Example v = new Example(2,4); v.display(3); } } a. 12 b. 8 c. 6 d. 3 9) ________ will be automatically invoked when an object is created. a. Constructor b. Method c. Function d. main method 10) “this” keyword is used to ________ a. refer to the object of super class b. refer to the current object c. refer to the object of sub class d. refer to the object of “Object” class 11) What happens if you don't put a constructor in a class definition? a. The class creates its own with arguments for every variable. b. The class creates its own with arguments for every method. c. The class creates its own without any arguments d. You get an error message 12) Given the following code: class First { int i ; int j ; int k; } Which is used to create the object? a. First f = First() b. First f = First c. First f = new First d. First f = new First() 13) You would use the ____ operator to test whether the object is an instance of the specified type (class or interface)? a. new b. dot c. instanceof d. None of the others 14) What are true for class and object? (Choose 2)

a. A class is a blueprint that defines variables and methods being common to all objects of a certain kind b. An object is a data type from which classes are made c. A class is only like to an object d. An object is an instance of a class 15) Which option is false for class variable? a. Class variable is shared for every instance of the class. b. Class variable is defined inside methods, constructors or blocks. c. Class variable is static variable within a class but outside any method. d. Class variables can also be manipulated without creating an instance of the class. 16) What is the output from the following code: public class Outer { static { System.out.print("Starting"); } private int x = 100; class Inner { private int y = 10; void display_x() { System.out.print(x); } } void test() { Inner inner = new Inner(); inner.display_x(); } void display_y() { System.out.print(y); } public static void main(String args[]) { Outer outer = new Outer(); outer.test(); outer.display_y(); } } a. Starting100 b. Starting10010 c. Compile-time error: cannot find symbol y in outer class d. Compile-time error: cannot use static block in outer class 17) You have used the following declaration in your Java program: class MyClass { float aFloat; }. Which type of variable have you declared? a. A final variable b. A static variable. c. An instance variable d. A class variable 18) Which one of the following options should we use to declare a variable such that there is only a single copy of the variable and it is shared by every object instantiated from the class. a. Instance variable

b. Final variable. c. Static variable. d. Temporary variable 19) The term "object variable" is another name for ____, and the term "class variable" is another name for _____. a. static field, object field b. instance field, static field c. default field, public field d. member field, static field 20) Which of following operator is used to allocate memory to array variable in Java? a. new b. malloc c. calloc d. none of the others 21) public class A { void m() { System.out.println("This is class A") ;} } public class B { void m() { System.out.println("This is class B") ;} public static void main( String []args) { A x=new B(); ((B)x).m(); } } Choose a right statement about the code. a. class B can not be converted to class A. b. you must use the try ...catch when convert datatype. c. This is class A d. This is class B 22) Given the following class definition public class Dog { private String name; //constructor… public void setName(String name) { //???? } } Which of the following statement is suitable for the implementation of setName() method? a. name = this.name; b. newName = name; c. this.name = name; d. name == newName; 23) What does the default access modifier mean? a. It provides access to any class in the same package. b. It means as the private modifier. c. It can provide access to any class in the different packages which is a super class.

d. It means as the protected modifier. 24) Consider the following code statements

public static void main(String[] args){ Dog d1 = new Dog(); d1.setName(“Milu”); Dog d2 = d1; d2.setName(“Misa”); } After executing all, what is the name of the dog d1 and d2 (in this order)? Select one: a. Misa, Misa b. Misa, Milu c. Milu, Milu d. Milu, Misa 25) Which option is passed to the method by value a. String b. int c. float d. All of the others 26) Choose a right statement for following code? public class AA { static { System.out.println("This is a AA"); } static class BB{ public void m(){ System.out.println("This is a method in BB"); } } public static void main(String[] args) { BB x=new BB(); x.m(); } } This code will display: a. This is a AA This is a method in BB b. This is a method in BB c. syntax error d. Can not use the keyword "static" in nested classes 27) In the following Outer class definition, which variables are inaccessible within the method of the static nested class? (Choose 2) public class Outer { public int x = 3;

private int y = 4; public static int z = 1; private static int t = 2; public static class Nested { public void method() { //which variables are inaccessible } } } a. x b. y c. z d. t 28) The ___ is entry point of Java programs a. class method b. main method c. static block d. instance method 29) What is the output from the following code class Square { private int side; public Square() { System.out.print("This is a Square with edge = "); new Square(100); } public Square(int side) { System.out.println(side); } public static void main(String[] args) { new Square(); } } a. This is a Square with edge = b. This is a Square with edge = 0 c. This is a Square with edge = 100 d. Compile-time error 30) The _____ is special method. It’s code will execute when an object of this class is initialized. a. main method b. static method c. constructor d. standard method 31) Which keyword is used to refer current object of a class in Java? a. super b. new

c. current d. this 32) Software objects are conceptually similar to real-world objects. They all consist of state and related behavior. A software object stores its state in _____ and exposes its behavior through ______. a. Methods, fields b. Classes, objects c. Fields, methods d. None of the others 33) Which package is imported by default into Java programs? a. java.awt b. java.io c. java.util d. java.lang 34) Non-static functions can be accessed using a. Object b. Class c. Nothing d. Subclass 35) What is the output from the following code class NumberHolder { public int anInt; public float aFloat; public static void main(String[] args) { NumberHolder aNumberHolder; aNumberHolder.anInt = 1; aNumberHolder.aFloat = 2.3f; System.out.print(aNumberHolder.anInt); System.out.print(aNumberHolder.aFloat); } } a. 12.3 b. 12.3f c. 3.3 d. Error 36) What is not Encapsulation? (Choose 2) a. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. b. Encapsulation is a technique for protecting data from misuse by the outside world c. Encapsulation is the process of hiding the implementation details of an object from its user d. Encapsulation is the ability of an object to take on many forms.

e. Encapsulation is the process of identifying and grouping attributes and actions related to a particular entity as relevant to the application at hand f. Encapsulation is the process of combining elements to create a new element g. Encapsulation is the process by which we can control what parts of a program can access the members of a class 37) What is the output from the following code class Test { public static int x = 7; public int y = 3; public static void main(String[] args) { Test a = new Test(); Test b = new Test(); a.y = 5; b.y = 6; a.x = 1; b.x = 2; System.out.print(a.y); System.out.print(b.y); System.out.print(a.x); System.out.print(b.x); } } a. 5612 b. 5622 c. 3377 d. 7733 38) Given the following class definition public class Rectangle { //declare fields here… //more code here… } Choose the correct default constructor’s definition a. public Rectangle() { } b. public Rectangle() {//code here…} c. public void Rectangle() {//code here…} d. public Rectangle(String msg) {//code here…} 39) Suppose that all needed packages are imported Consider the below code: public class Point3D{ private int x, y, z; public Point3D(int x,int y,int ) { this.x=x; this.y=y;

this.z=z; } public void display(){ System.out.print(this); } public static void main(String[] args) { Point3D A=new Point3D(); A.display(); } } Choose a right statement for the above code. a. Create a Point3D object without any error b. display() method call error. c. Create a Point3D object with an error d. None of the others 40) The toString() method of a String object will return .... a. String@AddressOfTheString b. the content of the string. c. the memory address at which the content of the string is stored. d. None of the others. 41) How do you create an instance of Paragraph's Identation class? class Paragraph{ class Identation{ int left, right; public Identation(int left, int right){ this.left = left; this.right = right; } } } a. new Paragraph().new Identation(4,5); b. new Paragraph().Identation(4,5); c. Identation ident = Paragraph.new Identation(4,5); d. Paragraph.Identation ident = new Paragraph().Identation(4,5); 42) A variable declared with the default modifier can be accessed by _____ (Choose 2) a. different packages and different classes b. same package different classes c. same package sub classes d. different packages and sub classes 43) Which of the following statements regarding the ‘final’ modifier are true? (Choose 2) a. A variable defined as ‘final’ is a constant b. A class declared as being ‘final’ can be subclassed c. d ‘final’ can be overridden in the subclass d. A class declared as being ‘final’ can be subclass 44) What will be the output of the following code?

public class integerequals { public static void main (String args[]) { Integer a= new Integer(0); Integer b= new Integer(0); System.out.println(a==b); } } a. The compiler will show an error b. The program compiles and prints true c. The program compiles and prints false d. The program compiles but causes a runtime exception 45)

a. Hello5 b. 5Hello c. 5 d. Hello 46) Consider the following classes public class Test { public static void test() { this.print(); } public static void print() { System.out.println("Test"); } public static void main(String args []) { test(); } } What is the result of compiling and running this class? a. The string Test is printed to the standard out. b. Nothing is printed to the standard output. c. An exception is raised stating that the method test cannot be found. d. An exception is raised stating that the variable this can only be used within an instance. e. The class fails to compile stating that the variable this is undefined 47) In the following class definition, which variables are inaccessible within the method of the inner class? class Test1 { public static int a = 1; private static int b = 2; public int c = 3; private int d = 4;

public static class A { int e = 5; public void aMethod(int f){ int g = 6; // What can't be accessed here? } } } a. b, c, d b. c, d c. b, c, d, f d. None of them 48) What is the output of following code? class Ex { private int x, y; public Ex(int x, int y) { this.x = x; this.y = y; } public void display(int y) { System.out.println(x*y); } public static void main(String args[]) { Ex t = new Ex(4,5); t.display(2); } } a. 8 b. 20 c. 10 d. 2 49) What will happen when you attempt to compile and run the following code? public class Static { static { int x = 5; } static int x,y; public static void main(String args[]) { x--; myMethod(); System.out.println(x + y + ++x); } public static void myMethod() { y = x++ + ++x;

} } a. Compiletime error b. prints : 1 c. prints : 2 d. prints : 3 e. prints : 7 f. prints : 8 50) Given the following code, what will be the output? class Value { public int i = 15; } public class Test { public static void main(String argv[]) { Test t = new Test(); t.first(); } public void first() { int i = 5; Value v = new Value(); v.i = 25; second(v, i); System.out.println(v.i); } public void second(Value v, int i) { i = 0; v.i = 20; Value val = new Value(); v = val; System.out.println(v.i + "" "" + i); } } a. 15 0 20 b. 15 0 15 c. 20 0 20 d. 0 15 20 51) What is the output from the following code class Person{ String name; int yob;

public Person(String name, int yob) { this.name = name; if (yob...


Similar Free PDFs