Question

In: Computer Science

1. A constructor is a special Class member method. It is automatically called when an object...

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

Solutions

Expert Solution

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.


Related Solutions

Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type)...
Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color...
package hw; public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically...
package hw; public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[5]; } public MyArrayForDouble(int capacity) { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[capacity]; } public MyArrayForDouble(double[] nums1) { nums = new double[nums1.length]; for(int i=0;i<nums1.length;i++) nums[i] = nums1[i]; numElements = nums1.length; } void printArray(){ // cost, times System.out.printf("printArray(%d,%d): ",numElements,nums.length); for(int i=0; i<numElements;i++) System.out.print(nums[i]+" "); System.out.println(); }...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
Project 1 Fractional Number Class Part-1 for Five groups of member functions: Constructors (inc. string constructor)...
Project 1 Fractional Number Class Part-1 for Five groups of member functions: Constructors (inc. string constructor) Getter/setters Math Type cast Friend << and >> Part-2 for the explanation for the behavior observed by running the provided test pattern: six (6) operations in one statement vs. six (6) operations in six (6) statements. PROJECT 1 Frac Class The following information, located on Github m03, are provided as a starter. Frac.h, a complete Frac Class Declaration ( the definition is to be...
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson copy constructor example.
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .2. Fill in the blanks so that the statement will instantiate a JButton object and assigns it to variable of JButton type:JButton btn= _______ ("OK");3. Fill in the blanks to complete the description of the constructor in the corresponding UML class diagram:+<>______( _____ : ______)
Create a C++ class with a static member item so that whenever a new object is...
Create a C++ class with a static member item so that whenever a new object is created, the total number of objects of the class can be reported.
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp....
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp. Constructor checks if the temperature is between 0 and 500. If the temperature does not fit the requirement, then set the temperature to be zero. Otherwise, set it to be the value passed to the constructor. Set the power to be off. Set the String to be the color passed to the constructor. Use the same requirement for the constructor for the set temperature...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT