Test 2008 Midterm Spring Sample Instructions: This is a closed book, notes, neighbor examination. Select the best answer. No, it is not a typo, all code appears as intended. Any text in this font is as intended. Do not ask the instructor about any "typos" that is in this font. All output is to be considered what appears in the console. DO write on the examination. 1. Two different objects belonging to the same class: A. Share data B. Share the structure. They can share data too, through the static keyword C. Share data and methods D. Share only methods E. 2. In Java, if no access specifier is placed in front of a class member what will be the access to that member? A. public, everyone can access the member B. friendly, the other classes in the same package can access the member C. private, only the class can access the member D. protected, only the class and any derived classes can access the member E. None of the above 3. What makes difference between Composition versus Inheritance A. Object created from a existing Class towards a New Class is called composition . Object created from a existing type Class with modification to some of their attributes B. Object created from a existing Class towards a New Class with modification to some of their attributes is called composition . Object created from a existing Class is called Inheritance C. Object created from a existing Class towards a New Class is called composition . Object created from a existing Class is called Inheritance D. Composition and Inheritance as same . E. None of the above 4. What results from attempting to compile and run the following code? public class Ternary { public static void main(String args[]) { int a = 5; System.out.println("Value is - " + ((a < 5) ? 9.9 : 9)); } } A. Prints: Value is - 9 B. Prints: Value is - 5 C. Compilation error D. None of these E. 5. The class Child inherits data and method members for the class Parent. The proper syntax for writing the beginning of the child class is : A. class Child inherits Parent { ... } B. class Child {Parent} { ... } C. class Child extends Parent { ... } D. All of the above. E. None of the above 6. What is displayed when the following is executed? class Parent { private void method1() { System.out.println("Parent's method1()"); } public void method2() { System.out.println("Parent's method2()"); method1(); } } class Child extends Parent { public void method1() { System.out.println("Child's method1()"); } public static void main(String args[]) { Parent p = new Child(); p.method2(); } } A. Compile time error B. Run time error C. prints : Parent's method2() Parent's method1() D. prints : Parent's method2() Child's method1() E. 7. What is the difference between inheritance and composition in object oriented programming. A. inheritance: money, property,and Java codes that you receive from someone who has died. composition: a short piece of writing about object oriented programming. B. inheritance: is_a relationship. You're taking a general purpose class and specializing it for a particular need. composition: has_a relationship. You want the features of an existing class inside your new class, but not interface. C. inheritance:has_a relationship. you want the features of an existing class inside your new class, but not interface. composition: is_a relationship. You're taking a general purpose class and specializing it for a particular need. D. inheritance: the derived class can not access the "protected" and "private" data member and method member of the base class. composition: Inside your new class you can access the "protected" data member or method member of the existing class E. None of the above 8. class A { A() {} ... } class B extends A { B() {} ... } class C extend B { C() {} ... } Somewhere in main: C x = new C(); In what order will the constructors be called? A. Class A's constructor Class B's constructor Class C's constructor B. Class C's constructor Class B's constructor Class A's constructor C. Only C's constructor will be called D. No constructors will be called. E. None of the above 9. What is the output of the following code? public class PrivateOverride { private void f( ) { System.out.println("private f( )"); } public static void main(String args[]) { PrivateOverride po = new Derived( ); po.f( ); } } class Derived extends PrivateOverride { public void f( ) { System.out.println("public f( )"); } } A. public f( ) B. private f( ) C. No output -- Causes compiler error because there are 2 methods of the same name with the same parameter lists D. No output -- Causes compiler error because overriding private methods of a derived class is not allowed E. None of the above 10. The Java language and libraries from Sun Microsystems are: A. available for a low cost to students B. cannot be used without a run time license fee C. free and can be downloaded at http://java.sun.com D. are supported only on Microsoft XP E. None of the above 11. Which statement is not a basic characteristic of object-oriented programming in Java? A. Everything is an object and Every object has a type. B. A program is a bunch of objects telling each other what to do by sending messages. C. Each object has its own memory made up of other Objects. D. All objects of a particular type can receive the same messages. E. None of the above 12. Java 2 refers to which JDK(s): A. JDK 1.1 or greater B. JDK 1.2 or greater C. JDK 2.0 or greater D. JDK 2.3 or greater E. None of the above 13. What will happen when you attempt to compile and run the following code? class Base { int i = 99; public void amethod() { System.out.println("Base.amethod()"); } Base() { amethod(); } } public class Derived extends Base { int i = -1; public static void main(String argv[]) { Base b = new Derived(); System.out.println(b.i); b.amethod(); } public void amethod() { System.out.println("Derived.amethod()"); } } A. Derived.amethod() -1 Derived.amethod() B. Derived.amethod() 99 Derived.amethod() C. 99 Derived.amethod() D. Compile time error @ E. 14. Where can the keyword final be used? A. on data members B. on methods C. on classes D. All of the above E. None of the above 15. What will be the result of executing the following code? 1: public static void main(String args[]) { 2: char digit = 'a'; 3: for (int i = 0; i < 10; i++) { 4: switch (digit) { 5: case 'x': { 6: int j = 0; 7: System.out.println(j); 8: } 9: default: { 10: int j = 100; 11: System.out.println(j); 12: } 13: } 14: } 15: int i = j; 16: System.out.println(i); 17: } A. 100 will be printed 11 times. B. 100 will be printed 10 times and then there will be a runtime exception. C. The code will not compile because the variable i cannot be declared twice within the main() method. D. The code will not compile because the variable j cannot be declared twice within the switch statement. E. None of the above. 16. All of the following are true, except: A. When you start a design in object oriented programming, you should generally prefer composition during the first version because it is more flexible than inheritance. B. Use inheritance only when it is clearly necessary. C. By using inheritance, you can change the behavior of composed objects at run-time. D. By rapidly developing using composition and inheritance, you will need to redesign the class hierarchy before allowing programmers to become dependent on it. E. None of the above 17. Which of the following options will run the PropertyCheck class and produce the output SCJA ? public class PropertyCheck { public static void main(String args[]) { String myprop = System.getProperty("myprop"); System.out.println(myprop); } } A. java -Dmyprop = SCJA PropertyCheck B. java PropertyCheck -Dmyprop=SCJA C. java -Dmyprop=SCJA PropertyCheck D. java - Dmyprop=SCJA PropertyCheck E. java -Dmyprop=SCJA PropertyCheck.class 18. C# and the .NET platform A. may be run on many different operating systems including Microsoft Window XP, Linux, and Apple OS X B. have made Java obsolete C. compete with Java on Linux platforms D. are a Microsoft competitive response to Java and have some of the same advantages as Java has over C++ E. None of the above 19. Polymorphism: A. Is highly utilized in procedural programming. B. Is commonly known as method overloading in Java. C. Must not be used with data abstraction or inheritance. D. Causes code to be more complicated and reduces readability. E. None of the above 20. What results from the following code? 1: class MyClass { 2: void myMethod(int i) { 3: System.out.println("int version"); 4: } 5: void myMethod(String s) { 6: System.out.println("String version"); 7: } 8: public static void main(String args[]) { 9: MyClass obj = new MyClass(); 10: char ch = 'c'; 11: obj.myMethod(ch); 12: } 13: } A. Line 5 will not compile as void methods can't be overridden. B. An exception at line 11 C. Line 11 will not compile as there is no version of myMethod, which takes a char as argument. D. The code compiles and produces output: int version. E. The code compiles and produces output: String version. 21. A class with default access appears as what type of class to external packages? A. Friendly B. Public C. Private D. Protected E. None of the above 22. How to Avoid Collisions of Classes when two libraries are imported via * ? A. Use class names only B. Use class names including their qualification to avoid Collisions, like java.util.count c = new java.util.Count(); C. Use class name with a custom Tool library D. Use custom Tool library E. None of the above 23. In method binding, which of the following statement is false? A. Early binding is when the binding is performed before the program is run. B. All method binding in Java uses late binding whether or not a method is declared final because ordinarily it will occur and happens automatically. C. Late binding means that the binding occurs at run-time based on the type of object. D. All method binding in Java uses late binding unless a method has been declared final. E. None of the above 24. Which of the following is NOT an access specifier for Java? A. public B. protected C. friendless D. private E. None of the above 25. The data member VALUE in the statement public static final int VALUE = 100;... A. is usable outside the package, exists only once, and is a constant int. B. can be changed at runtime C. is not a compile time constant D. is a string E. None of the above 26. A variable that is declared in a class (and not in any method) is referred to as a A. member variable B. static member variable C. local variable D. static local variable E. None of the above 27. Which one of the following operators have the highest precedence? A. Arithmetic (and shift) * / % + - << >> B. Relational > < >= <= == != C. Logical (and bitwise) && || & | ^ D. Unary + - ++ -E. None of the above 28. Members with which access specifier are accessible only to the methods of that class? A. private B. public C. friendly D. protected E. None of the above 29. The garbage collector: A. Is a feature of Java. B. Finds objects no longer being referenced and releases the memory used by those objects. C. Makes programming in Java easier than C++. D. All of the above. E. None of the above 30. If no access specifier is used, what is the default? A. friendly B. private C. public D. protected E. None of the above 31. Java was created as a more efficient version of what other programming language? A. PASCAL B. C C. FORTRAN D. C++ E. None of the above 32. Supporting polymorphism requires that Java implement: A. late (aka run time) binding. B. early binding. C. book binding. D. All of the above E. None of the above 33. How does Java avoid the issue of name collision? A. By building classes into libraries. B. By making all names of all files the same or similar. C. By allowing the library designer to change the internal workings of the class without worrying about how it will affect the client programmer. D. By controlling the names with the package keyword, the package naming scheme, and the import keyword. E. None of the above 34. In the following code, what does the keyword "final" indicate? void convert(final int feet, int inches){ //... } A. The data in the "feet" argument cannot be modified within the method. B. The "feet" argument must be initialized immediately within the method. C. The method cannot be called from another package. D. The argument name "feet" cannot be changed. E. None of the above 35. The final keyword can be used with: A. Compile and run time constants which are primitives and object references. B. Methods to prevent an inheriting class from changing its meaning. C. Classes to disallow inheritance. D. All of the above E. None of the above 36. Java is most closely related to: A. Javascript B. C/C++ C. BASIC D. Visual Basic E. None of the above 37. What is function overloading? A. Happens when you declare too many functions in a Java block of code. In java this limit is fixed to 65536 functions B. It's the assigning of many tasks to one (or more) methods. It's an OOP milestone C. It's the assigning of the same name to many methods. It can be useful in presence of similar methods which behaves differently or have different parameters D. 38. What happens in this code fragment? 1: 2: 3: 4: 5: 6: 7: ... String a = new String("Test"); String b = new String("test"); if (a==b) System.out.println(" Same"); else System.out.println(" Different"); A. Prints "Different", because a and b points to strings which are different for letter casing, and Java is case-sensitive B. Prints "Same", because the "==" operator is overridden in the String class C. Prints "Different", and it should do the same with any value D. Prints "Same", and it should do the same with any value E. None of the above. 39. When an object of a derived class is created, the default constructor for the base class is called: A. always, and before the constructor for the derived class. B. always, and after the constructor for the derived class. C. only if it explicitly called by the derived class constructor. D. only if no other base class constructor is called by the derived class constructor. E. None of the above 40. Java uses dynamic binding which A. occurs during compile time B. occurs at run-time C. occurs after compilation and before file linking D. occurs during an object upcast E. None of the above 41. Which of the following is NOT true with respect to Java access modifiers A. private methods are accessible from only that class which defined the method B. public methods can be accessed from other classes C. protected methods can be accessed from the class which defined the method and also from the classes derived from it. D. Friendly classes can be accessed from all the classes in the current package. E. None of the above 42. Which of the following is NOT true about cleanup and the garbage collector? A. Java does have the C++ concept of a destructor. B. In Java, the practice is to forget about objects rather than destroy them. C. If you want to do your own cleanup, you need to explicitly write a special method to do it. D. To guard against an exception, you must put any cleanup in a finally clause. E. None of the above 43. Which of the following statements is true? A. You can only have one main method per file. B. You can only have one class per file. C. You can have only one public class per file D. You can have as many public classes per file as required E. None of the above 44. Where can the key word final be used? A. on member variables B. on member methods C. on class definitions D. All of the above E. None of the above 45. Like any Human language Java provides a way to A. stay awake at night B. express concepts C. wake up in the morning D. talk to people from long distances E. None of the above 46. Polymorphism is also known as: A. Dynamic binding B. Late binding C. Run-time binding D. All of the above E. None of the above 47. Assume you have just entered some Java code into a source file with no public classes. You now wish to save a compile it. Which of the following will work? A. Save it in a file called MyClass.jav and compile it with javac MyClass.jav B. Save it in a file called 1999Work.java and compile it with javac 1999Work.java C. Save it in a file called MyClass.java and compile it with javac MyClass D. All of the above E. None of the above 48. Given: 1: package payroll; 2: final public class EmployeeNames { 3: public static final String[] names = {"Paul", "Adam", "Jansky"}; 4: public static String[] getNames() { 5: return names; 6: } 7: } And the following: 8: package client; 9: class TraderNames extends EmployeeNames { 10: public static String[] traders = {"Ziggy", "Niko", "Chris"}; 11: } Which of the following statements is true? A. The file with class EmployeeNames will not compile. B. The file with class TraderNames will not compile. C. The files will both compile, but an error will occur when the class TraderNames is instantiated. D. The files will compile and when either class is instantiated, it will work fine. E. None of the above. 49. What will happen when you attempt to compile and run the following code snippet? public static void main(String[] args) { Boolean b = new Boolean("TRUE"); if (b.booleanValue()) { System.out.println("Yes : " + b); } else { System.out.println("No : " + b); } } A. The code will not compile. B. It will print - Yes : true C. It will print - Yes : TRUE D. It will print - No : false E. It will print - No : FALSE 50. The Creator of Java is? A. Dennis M Ritchie B. James Gosling C. Bruce Eckel D. Brian W. Kernighan E. None of the above 51. the Java programming language is derived from A. Pearl B. C/C++ C. BASIC D. JavaScript E. None of the above 52. What is connecting a method call to a method body is called? A. Binding B. Wind C. Stringed D. Brass E. None of the above 53. What will be printed when you execute the following code? class X { Y b = new Y(); X() { System.out.print("X"); } } class Y { Y() { System.out.print("Y"); } } public class Z extends X { Y y = new Y(); Z() { System.out.print("Z"); } public static void main(String[] args) { new Z(); } } A. Z B. YZ C. XYZ D. YXYZ E. None of the above. 54. What is pure substitution? A. Substitution of one method for another with exactly the same interface. B. A derived class that has exactly the same interface as the base class. C. Substitution of one plugin for another with exactly the same functionality. D. A derived class with exactly the same methodology as another derived class. E. None of the above 55. Package access allows A. access from any private class. B. access from any class in the same library. C. access from any class in the same directory. D. B and C. E. None of the above 56. What is the order of constructor calls for a complex object? A. 1. The body of the derived-class constructor is called. 2. Member initializers called in order of declaration. 3. Base class constructor is called. B. 1. Base class constructor is called. 2. Member initializers called in order of declaration. 3. The body of the derived-class constructor is called. C. 1. Member initializers called in order of declaration. 2. Base class constructor is called. 3. The body of the derived-class constructor is called. D. 1. Member initializers called in order of declaration. 2. The body of the derived-class constructor is called. 3. Base class constructor is called. E. None of the above 57. Which of the following statements are true? A. A constructor may not be declared as private B. A Constructor with no arguments will be the default constructor C. Constructors cannot be overloaded D. A call to a constructor in a parent class can only be made from within a constructor. E. None of the above. 58. Without exception, in Java a compilation unit(file) must have: A. A public class with the same name as the file. B. A private class with the same name as the file. C. A public class with a constructor name the same as the file. D. A public class with the same name as the file and a main method. E. None of the above 59. Can methods be overloaded on return values? A. No. Because all methods have a unique name. B. Yes. You can overload methods using return values. C. No. Because methods can be called ignoring the return value. D. No. Because this leads to ambiguous code. E. None of the above 60. Any private methods in a class A. must be explicitly final. B. are final, but can be overridden. C. are implicitly final and cannot be overridden. D. are implicitly overridden. E. None of the above 61. What will be the result of executing the following code? 1: boolean a = true; 2: boolean b = false; 3: boolean c = true; 4: if (a == true) 5: if (b == true) 6: if (c == true) 7: System.out.println("Some things are true in this world"); 8: else 9: System.out.println("Nothing is true in this world!"); 10: else if (a && (b = c)) 11: System.out.println("It's too confusing to tell what is true and what is false"); 12: else 13: System.out.println("Hey this won't compile"); A. The code won't compile B. "Some things are true in this world" will be printed C. "Hey this won't compile" will be printed D. None of these E. 62. Which of the following ways to grant access to class members is considered "fundemental" to Java Beans? A. Java Bean Access Specifier B. Default Access Methods C. Accesor/Mutator Methods D. Public Access Specifier E. None of the above 63. What happens here? import java.util.*; public class ch3 { public static void main (String[] args) { int i = 'a' + 1; char c = 'b'; if ((char)i = c) { System.out.println ("yup"); } else { System.out.println ("nope"); } } } A. It prints the string 'yup' because 'a'+1 is in fact equal to 'b'. B. C. D. E. It won't compile, you can't add 1 to a character You can't cast an integer to a character in the if statement The if statement won't compile because it requires a boolean expression. None of the above 64. All classes of the java.lang package are available by default and need no special import statements. A. The statement is true. B. The statement is false. C. 65. What does friendly mean. A. All the other class in the current package and all other classes outside of this package have access to the friendly member. B. All the other class in the current package have access to the friendly member, but to all other classes outside of this package it appears as private. C. All other classes outside of the current package have access to the friendly member. D. Some the other class in the current package have access to the friendly member, but to all other classes outside of this package it appears as private. E. None of the above 66. What is the output for the following? class Vehicle { Vehicle() { System.out.println("Vehicle constructor"); } } class Chevy extends Vehicle { Chevy() { System.out.println("Chevy constructor"); } } public class Pickup extends Chevy { Pickup() { System.out.println("Pickup constructor"); } public static void main(String[] args) { Pickup p = new Pickup(); } } A. Vehicle constructor Chevy constructor Pickup constructor B. Pickup constructor Chevy constructor Vehicle constructor C. Pickup constructor D. Pickup constructor Chevy constructor E. None of the above 67. A class member with no access specifier: A. is available to any other class. B. is only available to mutator or accessor functions. C. is only available to classes derived from the class of which it is a member. D. is available to classes in the same package as the class of which it is a member. E. None of the above 68. What is the meaning of protected in Java programming language? A. It deals with a concept called inheritance, which takes an existing class and adds new members to that class without touching the existing class, which we refer to as the base class. B. "Sort of friendly" which takes an existing class and adds old members to that class without touching the existing class, which we refer to as the base class. C. It deals with a concept called inheritance, which takes an existing class and adds new members to that class with touching the existing class. D. It is a concept called inheritance, which create a class and adds new members to that class without touching other objects, which we refer to as the base class. E. None of the above 69. Which of the following statements are true? A. There can be only one public class per compilation unit (file). B. If you have more than one public class in a compilation unit, the compiler will give an error message. C. The name of the public class must exactly match the name of the file containing the compilation unit, including the capitalization. D. All of the above. E. None of the above 70. Which of the following types of access does *not* have a keyword? A. public B. protected C. private D. friendly E. None of the above 71. Given the following output: I is 0 I is 1 Which of the following snippets of code will cause the output? A. for( int I = -1; I < 2; ++I) { System.out.println("I is " + } B. for( int I = 1; I < 3; ++I) { System.out.println("I is " + } C. for( int I = 0; I > 2; ++I) { System.out.println("I is " + } D. for( int I = 0; I < 2; ++I) { System.out.println("I is " + } E. None of the above I); I); I); I); 72. How does Java avoid name collisions?: A. The package keyword, the package naming scheme, and the import keyword give you complete control over names. B. The US Government Bureau of Java Names assigns unique class and package names for a small fee. C. Since Java is a compiled language, there is no need to worry about names. D. The programmers on a project have to coordinate names among themselves. E. None of the above 73. Which of these is not a primitive type A. char B. int C. boolean D. String E. None of the above 74. Polymorphism is also called: A. run-time binding B. late binding C. dynamic binding D. all of the above E. None of the above 75. In Java, scoping refers to: A. the algorithm used by the garbage collector to reclaim memory B. the use of mouthwash prior to prolonged programming episodes C. the visibility and lifetime of objects and variables D. the visibility of private and public variables inside a class E. None of the above 76. A class must be qualified as abstract: A. if all the methods it contains are abstract. B. if one of the methods it contains are abstract. C. if the class is derived from an abstract class and does not contain a definition for one of the base class abstract methods. D. all of the above. E. None of the above 77. Given the code: outter_loop { inner_loop { //A break; //B continue; //C continue something; //D break something; } } Which statement resumes processing at the beginning of the inner_loop? A. break; B. continue; C. continue something; D. break something; E. None of the above 78. What does OOP stand for? A. Open Object Programming B. Object Oriented Programming C. Original Operating Parameters D. Ordinary Object Persistence E. None of the above 79. What is the purpose of including the protected keyword in Java? A. To grant access of particular non public member(s) to non derived classes. B. To grant access of particular non public member(s) to derived classes. C. To deny access of particular public member(s) to non derived classes. D. To deny access of particular public member(s) to derived classes. E. None of the above 80. Which of the components of OOP allows you to create a family with the same interface? A. Polymorphism B. Inheritance C. Composition D. Abstraction E. None of the above 81. What is method overriding? A. An overloading alias B. It doesn't exists, you invented it C. Redefining a method inherited from superclass, you specify its tasks D. Redefining a method inherited from superclass, you generalize its tasks E. 82. What keyword is used to call the base-class constructor that has an argument? A. top B. super C. big D. base E. None of the above 83. The order of initialization when the class is inherited from a base class is A. the base class is initialized before the derived class constructors. B. the derived class constructors are intilized and then the base class constructor C. constructor needs to be called explicit with super keyword D. constructors will not be called when the class is inherited. E. None of the above 84. Java uses three explicit keywords to set the boundaries in a class, which of the following is not one of the three keywords? A. public B. private C. personal D. protected E. None of the above 85. Does a class that inherits methods from another class have any special or additional access or privileges to the 'private' methods of the inherited class? A. Yes, but it is restricted to read only access. B. Yes, it has full access. C. No, it has no access. D. It depends on how you inherit the methods. E. None of the above 86. Primitives that are fields in a class are automatically initialized to zero, but the object references are initialized to null. If you try to call methods for any of them you’ll get an exception. If you want the references initialized, you can do it legally except: A. At the stage where the objects are defined, which means that they always be initialized before the constructor is called. B. Right after you use the object, which is often called post-reference initialization. C. In the constructor for that class. D. Right before you actually need to use the object, which is often called lazy initialization. E. None of the above 87. When using Inheritance, the base class is initialized A. before the derived class can access it. B. simultaneously with the derived class. C. after the derived class is initialized. D. by explicitly writing the call to the base class constructor even if the constructor has no arguments. E. None of the above 88. Describes some thing you know about package. A. Because Java use package statement, class name collision will never happen. B. package is a key word; a package statement must appear at the first non-comment code in the file. C. package is a key word; a package statement must appear at the end of the file. D. package is a key word; a package statement may appear at any place you wish in the file. E. None of the above
© Copyright 2024