Consider the array declaration and initialization: double []x = new double [6];
ID: 3821961 • Letter: C
Question
Consider the array declaration and initialization: double []x = new double [6]; x[] = {12, -1, 15, 3, 99, -5}; (a) What is the type of x[6]? (b) What is the value of x length? (c) What is the value of x[4]? (d) What is the largest index i such that x[i] is valid? (e) What is the value of smallest valid index of x[i]? Suppose you are designing a class. (a) Another name for the getters is _____? (b) Methods of a class are called class _____ (c) The method with the class name and no return type is _____ (c) (d) Other than the constructor, getters & setters you need a _____ (e) The methods that update the objects data values are _____? What is the output of this program? public class Q_08 {public static void main(String[] args) {int x = 2 for(int i = 1; iExplanation / Answer
5) Question:
a) Accessor Methods
Accessor Methods are also called Getters
b) Instance Methods
Instance Methods are accessed by object only, which is instance of class.
c) Constructor
Whenever object is created constructor is called, hence the return type of that is always Object type.
d) Instance Variables, Blocks , main method() and object
A class contains, Properties, Methods, Constructors, Blocks.
e) Setters
Setters are also called Mutators
Example:
public class Student {
String id,name; // Properties
//Constructor
public Student(String id,String name){
this.id = id;
this.name = name;
}
//Getters
public String getId() {
return id;
}
//Setters
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
static { // Block
System.out.println("Student Info");
}
}
6) Question:
public class Q_08 {
public static void main(String[] args) {
int x=2;
for(int i=1;i<=5;i++){
x=i%x;
System.out.println("i="+i+" x="+x);
}
System.out.println("The final answer is:"+x);
}
}
Output:
i=1 x=1
i=2 x=0
Exception in thread "main" java.lang.ArithmeticException: / by zero
Explaination:
For the first iteration of loop when i=1, x=2, x = i%x, the x=1
So, it prints i=1 x=1
For the second iteration of loop when i=2, x=1, x = i%x, the x=0
So, it prints i=2 x=0
For the third iteration of loop when i=2, x=0, x = i%x, the x=Undetermined
Since, Divide By Zero is undetermined, and Java JRE throws RuntimeException called ArithmeticException
4) Question
If we consider the given question stores array of 6 double values, the below are the answers for them:
double []x = new double[6];
x[] = {12,-1,15,3,99,-5}; //Invalid Syntax in Java
OR
double []x = new double[] {12,-1,15,3,99,-5}; //Valid One
a) There will be no 6th Value, since array starts from 0 to n-1, it throws ArrayIndexOutOfBoundsException
b) The value of x.length is 6
c) The value of x[4] is 99.0
d) The Largest Valid index is 5
e) The smallest valid Index is 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.