Docsity

Java Programming: Static Variables and Methods - Review Questions, Exams of Advanced Education

Toggle sidebar
bg1
Unit 5 Lesson 8 Review Questions
Which of the following is true about static variables? - Static variables can be accessed without creating
an instance of a class.
Consider the following class.
public class ClassicClass
{
private static int count = 0;
private int num;
public ClassicClass()
{
count++;
num = count;
}
public String toString()
{
return count + " " + num;
}
}
What is printed when the following code in the main method of another class is run?
ClassicClass a = new ClassicClass();
ClassicClass b = new ClassicClass();
ClassicClass c = new ClassicClass();
System.out.println(a + ", " + b); - 3132
Here is a description of variables used in a car wash class.
customer - represents the name of the person paying for the car wash
vehicle - represents the type of vehicle of the customer
pf2

Partial preview of the text

Download Java Programming: Static Variables and Methods - Review Questions and more Exams Advanced Education in PDF only on Docsity!

Unit 5 Lesson 8 Review Questions

Which of the following is true about static variables? - Static variables can be accessed without creating an instance of a class. Consider the following class. public class ClassicClass { private static int count = 0; private int num; public ClassicClass() { count++; num = count; } public String toString() { return count + " " + num; } } What is printed when the following code in the main method of another class is run? ClassicClass a = new ClassicClass(); ClassicClass b = new ClassicClass(); ClassicClass c = new ClassicClass(); System.out.println(a + ", " + b); - 3132 Here is a description of variables used in a car wash class. customer - represents the name of the person paying for the car wash vehicle - represents the type of vehicle of the customer

price - represents the cost per car wash on one specific day bill - represents the total amount of money owed by customer Based on these descriptions, which of the following variables would be static? - price You are writing a class for a district of schools with the same hours. Which of the following would you want to make static? - A boolean to determine if the schools are opened or closed. The following 2 questions refer to the class favNums which is defined below: public class FavNums{ private int num1; private int num2; public FavNums(int n1, int n2) { num1 = n1; num2 = n2; } public void printFavorites() { System.out.println(num1 + num2); } public static void printRandoms() { int r1 = (int)(100 * Math.random()); int r2 = (int)(100 * Math.random()); System.out.println(r1 + r2); }} Which of the following code segments show correct calls to the method printFavorites() which would compile and run in a method of a separate class? - FavNums f = new FavNums(1, 2); f.printFavorites(); Which of the following code segments show INCORRECT calls to the method printRandoms() which would compile and run in a method of a separate class? There may be more than one answer to this question - choose all that apply. - FavNums f = new FavNums(1, 2, 3); printRandoms(f);


printRandoms();