Java Questions

(1) 1. Which of the following statements are correct? [Select all correct answers] 1. Array indexes in Java always start at 0. 2. An instance variable declared in a class is created when an instance of the class is created. 3. An instance variable declared in a class is created the first time an instance of the class is accessed. 4. When an object is created, any instance variables which have not been assigned a value remain undefined. 5. The double and long primitive types are also known as literals. 2. Which of the following are not Java keywords? [Select all correct answers] 1. 2. 3. 4. extern long elsif case

3. Its not just Stateless Session Beans that are pooled, Entity Beans are as well. True/False? 1. True 2. False 4. Which of the following methods are invoked on a stateless session bean when it is in the method ready pool? 1. 2. 3. 4. 5. 6. 7. ejbCreate ejbActivate ejbPassivate setSessionContext unsetSessionContext business methods ejbRemove

5. What is the output of the following code?

1: 2: 3: 4: 5:

String str = "Welcome"; str.concat(" to Java!"); System.out.println(str); A) Strings are immutable, compilation error at line 3. B) Strings are immutable, runtime exception at line 3. C) Prints "Welcome". D) Prints "Welcome to Java!".

6. A class design requires that a particular member variable must be accessible for direct access by any subclasses of this class, otherwise not by classes which are not members of the same package. What should be done to achieve this? A. The variable should be marked public B. The variable should be marked private C. The variable should be marked protected D. The variable should have no special access modifier E. The variable should be marked private and an accessor method provided 7. The following declaration (as a member variable) is legal static final transient int _MaxElements = 100; A) True. B) False. 8. What is default layout manager for panels and applets? A) Flowlayout B) Gridlayout C) BorderLayout 9. Which of the following statements contains an error? A. B. C. D. SELECT * FROM emp WHERE empid = 345678; SELECT empid FROM emp WHERE empid = 345678; SELECT empid FROM emp; SELECT empid WHERE empid = 345678 AND lastname=’SMITH’;

10. Which statements are true about the collection interfaces? 1. Set extends Collection 2. All methods defined in Set are also defined in Collection

3. List extends Collection 4. All methods defined in List are also defined in Collection 5. Map extends Collection 11. Choose the legal array declarations. 1. int a []; 2. int [] a; 3. int [4] a []; 4. int a [5]; 5. int [] a [] []; 12. Which of the following statements regarding a web application deployed on J2EE server are true? Select three choices. 1. The configuration information is maintained in an XML file called web application deployment descriptor. 2. The web components are packaged in a file with war as an extension. 3. The web components are packaged in a file with jar as an extension. 4. The web application packages can be created using the war utility. 5. The web application packages can be created using jar utility. 13. You have been given a design document for a veterinary registration system for implementation in Java technology. It states: "A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet that has a flag indicating if it has been neutered, and a textual description of its markings." Given that the Pet class has already been defined, which of the following fields would be appropriate for inclusion in the Cat class as members? A. B. C. D. E. Pet thePet; Date registered; Date vaccinationDue; Cat theCat; boolean neutered; F. String markings;

14. Consider the following classes, declared in separate source files: 1. public class Base { 2. public void method(int i) { 3. System.out.println("Value is " + i); 4. } 5. } 1. public class Sub extends Base { 2. public void method(int j) { 3. System.out.println("This value is " + j); 4. } 5. public void method(String s) {

6. System.out.println("I was passed " + s); 7. } 8. public static void main(String args[]) { 9. Base b1 = new Base(); 10. Base b2 = new Sub(); 11. b1.method(5); 12. b2.method(6); 13. } 14. } What output results when the main method of the class Sub is run?
A. B. C. D. E. Value is 5 Value is 6 This value is 5 This value is 6 Value is 5 This value is 6 This value is 5 Value is 6 I was passed 5 I was passed 6

Answers (1)
1. Answers 1 and 2 are correct. 1 is correct because array indexes in Java always start at 0. Answer 2 is also correct. An instance variable is a field declared within a class declaration without the static keyword. When an instance of the class is created, any declared instance variables are also created and initialized to a default value. Answers 3 and 4 are incorrect because of the above statements. Answer 5 is incorrect because a literal is the source code representation of a value of a primitive type, the String type, or the null type. 2. Answers 1 and 3 are correct because extern and elsif are not Java keywords. Answers 2 and 4 are incorrect because they are valid Java keywords. 3. Correct answer is 1 (True). 4. Choices 6 & 7 are correct (business methods and ejbRemove) 5. Answer: C. Strings are immutable. So str.concat("to Java!") will not append anything to str. In fact it will create another string "Welcome to Java!" and leaves it. 6. Answer: C is correct(The variable should be marked protected) 7. Answer: A is correct. (True) 8. Answer: A is correct (FlowLayout) 9. Answer: D 10. Answer: 1, 2 and 3 are correct 11. Answers 1, 2 and 5 are correct. 3 and 4 are invalid because the array size may not be given when the array is declared. 12. Choices 1, 2 and 5 are correct. 13. Answer: E and F are correct. The Cat class is a subclass of the Pet class, and as such should extend Pet, rather than containing an instance of Pet. B and C should be members of the Pet class and as such are inherited into the Cat class; therefore, they should not be declared in the Cat class. D would declare a reference to an instance of the Cat class, which is not generally appropriate inside the Cat class itself (unless, perhaps, you were asked to give the Cat a member that refers to its mother). Finally, the neutered flag and markings descriptions, E and F, are the items called for by the specification; these are correct items.

14. Answer: C is correct. The first message is produced by the Base class when b1.method(5) is called and is therefore Value is 5. Despite variable b2 being declared as being of the Base class, the behavior that results when method() is invoked upon it is the behavior associated with class of the actual object, not with the type of the variable. Since the object is of class Sub, not of class Base, the second message is generated by line 3 of class Sub: This value is 6.

Additional verbal questions: • • • • How to make deprecation warnings appear? Nested try/catch blocks? How to make something printed after return statement in a method?

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.