In: Computer Science
1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and abstraction are both the pillars of Object-Oriented Programming. Abstraction means that a programmer hides all but the relevant data about an object in order to reduce the complexity. 5. The body of a while loop repeats as long as the loop condition is false, whereas the body of a do while loop repeats as long as the loop condition it true 6. Class member variables should be declared as private because the objectoriented programming principle of encapsulation. 7. The method public char getChar(); will return a character data. Page 2 8. Assume that there is a Class named Stock and the class has a constructor private Stock(String symbol, double price); So a programmer can create a Stock object by doing the following in the main method: Stock stock = new Stock(“Google Inc”, 578.45); 9. A breakpoint is a marker that you can set to specify where execution should pause when you are running your Java application in Eclipse. 10. We use int [] numbers to create a number of integer values in the heap. Section II: Multiple choices 11. What is output by the following code segment? boolean isHighTemp = true; double degree = 100.01; if (isHighTemp) if(degree >= 110) System.out.print(“Extremely Hot”); else System.out.println(“Not Hot”); A) Extremely Hot B) Not Hot C) Hot D) Extremely Hot Not Hot 12. To produce the output 2 4 6 8 10, what is the loop condition for the following loop? int n = 0; Page 3 do { n = n + 2; System.out.print(n + “ ”); }while (???) A) n < 10 B) n = 2 13. In Java, the value of the expression 23 % 6 is: A) 5 B) 2 C) 3 D) 4 14. Given the constant declaration final int CAPACITY = 10; which of the following is NOT a valid use of CAPACITY? A) CAPACITY += CAPACITY; B) CAPACITY = 100; C) int currentSize = CAPACITY + 9; D) A and B above 15. Given the following declaration and the method header for the Max method double x, y, z; Page 4 double Max(double, double); which of the following shows a syntactically invalid call to Max method: A) z = Max(x, y); B) z = Max((x+y+z); C) System.out.print(Max(x, (y+x) )); D) z = y + Max((y-x), (x+y)); 16. What is printed when the following code segment is executed? int i, j; for(i = 1; i numbers = new ArrayList(100); //line 1 numbers.add(8); //line 2 numbers.add(3); //line 3 numbers.add(4); //line 4 numbers.add(2, 5); //line 5 numbers.add(2, 6); //line 6 numbers.add(9); //line 7 numbers.add(2,10); //line 8 System.out.println(numbers.get(2)); //line 9 System.out.println(numbers.get(4)); //line 10 System.out.println(numbers.get(10)); //line 11 System.out.println(numbers.size()); //line 12 numbers.set(2, 4); //line 13 numbers.remove(3); //line 14 numbers.remove(4); //line 15 System.out.println(numbers.get(2)); //line 16 System.out.println(numbers.get(4)); //line 17 for(Integer i : numbers){ //line 18 System.out.print(i); //line 19 } } 35. What is the output on line 9? A) 3 B) 5 C) 10 D) None of above 36. What is the output on line 10? A) 3 B) 4 Page 15 C) 5 D) None of above 37. What is the output on line 11? A) 3 B) 4 C) 5 D) None of above 38. What is the output on line 12? A) 7 B) 4 C) 9 D) None of above 39. What is the output on line 16? A) 7 B) 4 C) 9 D) None of above 40. What is the output of the for loop on line 18 - 19? A) 83659 B) 83459 C) 84465 D) None of above
Answer:
1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created.
In the above statement , it is false that constructor can be called more than once, A constructor is called only once per object instance
2. In Java, the new operator is used to create an instance/object of a class in the heap.
It is true that new operator in java is used to create memory dynamically in the heap area of memory.
example: Student obj=new Student();
3. A reference variable is a memory location contains an address of an object stored in the stack.
It is false , A reference variable itself stored in the stack which has an address of an object which is stored in heap.
4. Encapsulation and abstraction are both the pillars of Object-Oriented Programming. Abstraction means that a programmer hides all but the relevant data about an object in order to reduce the complexity.
This statement is true , Abstraction is a process hiding all the Implementation deatails and showing only essential features.
5. The body of a while loop repeats as long as the loop condition is false, whereas the body of a do while loop repeats as long as the loop condition it true .
This statement is false, The body of a while loop repeates as long as loop condition is true, and do while loop first execute the loop body then check condtion and if it is true then this process repeat again. syntax given below.
while (condition) {
// Statements
}
do {
// Statements
}
while (condition);
6. Class member variables should be declared as private because the object oriented programming principle of encapsulation.
This statement is true , class member variable should be private So that our data will not flow free throught the program and will be safe from unusual changes in data.
you did not specified which question to answer out of 40 , So i answered first 6.
let me know if any doubt.