1/15
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What is the order of operations in Java?
It follows PEMDAS
Which of the following codes will assign 5 to the variable number and why?
I. int number = 79/15;
II. int number = 30 / 3 / (-3 + 5);
III. int number = 20 + 4/6 + 1
I and II only - I works because when 79/15 is performed, the number is 5.266667 - while this is a decimal, since the data type of number is an int, it will automatically assign the whole number value to the variable, which is 5. II also works because if the order of operations is followed, 5 is the answer. III is not valid because by following order of operations the answer would be 21.66667 - the integer number of this would still be 21, which is not 5.
What will be printed by the code segment below and why?
int first = 5;
int second = 10;
System.out.println(“t” + 9 + 1 + “ “);
System.out.println(“Go” + second + 5 + “ “);
System.out.println(second + first + “End”);
t91 Go105 15End. This is the answer because you must consider order of operations with the operations. Because 9 is added to String value “t”, the new value is a String: “t9”. Then 1 is added to “t9”, making the entire new value “t91”. It works similarly for the second line, the numbers are added first to a String. For the last line, however, a number is first added to a number, so the actual numerical value will be the new value when first and second are combined. Then finally, the String value is added to the end of that numerical value.
Which of the following statements would evaluate to 8.0?
Math.pow(2, 2) * Math.pow(2, 2) / 2;
Consider the following line of code:
int randomNumber = (int) (Math.random) ()* 20 + 2)
What are the possible values for the variable randomNumber?
All integers from 2 - 22 (not including 22).
The second number in the statement (2) represents the smallest possible number that the variable can be. Adding the first number in the statement (20) to the second number (2) in the statement represents the largest possible value that the variable can be (NOT inclusive). So in this case, the variable can hold a value from 2-22, but not including 22. (so basically 2-21)
What symbol creates comments in Java?
//
Write code which will calculate the volume of the sphere given the following formula:
V = 4/3 pi r³
Use the following variables representing the radius of the sphere, and pi. Store the answer in a variable called volume.
double pi = 3.1415;
double radius = 8.6;
double volume = (4.0/3.0 times pi) times (Math.pow(radius, 3));
you MUST write the numbers as 4.0 and 3.0 and NOT 4 and 3, because if you do so the numbers will simplify to an integer which would yield a different answer.
What is casting?
The process of converting one data type to another.
What is the value of var after executing the following statement?
double var = 12 / 5
2.0
Because the numbers in the variable are integers, the answer will simplify to just 2, but because it is stored in a double the answer will specifically be 2.0.
geometric mean question
III only. double value = v1 * v2; double geoMean = value.sqrt();
Consider the following code segment:
String oldStr = “ABCDEF”;
String newStr = old.Str.substring(1, 3) + oldStr.substring(4);
System.out.println(newStr);
What is the output?
BCEF; remember the 1st number shows where to start (inclusive), the second number shows the stopping point (NOT inclusive)
Book object question - Which of the following is the correct way to create an instance of the Book class?
Book myBook = new Book(“1984”, “George Orwell”);
Which of the following is true about overloading methods?
Defining multiple methods in the same class with the same name but different parameter lists.
output of same class code question
remember with the value of length, just start counting at 1, index does not matter. for substrings and indexes the index does matter.
Without showing the implementation write two methods which are overloaded and will compile without error:
public static int method(int x, int y);
{
}
public static String method(String a, String b);
{
}
To show overloaded methods that will compile without error, the parameters of each method must be different in some way. So the data types of the parameters in the second method must be different (int in the first method, String in the second), or the number of parameters must be different from the first method.
Consider the following code: (long bookStore code) and create two bookStoreReceipt objects, assuming they are in the main class.
BookstoreReceipt book1 = new BookstoreReceipt(“Keethu,” 19, “Thank you.”);
BookstoreReceipt book2 = new BookstoreReceipt(“Veronica,” 16, “Happy reading!”);