1.2.2 Variables and Types

studied byStudied by 15 people
0.0(0)
learn
LearnA personalized and smart learning plan
exam
Practice TestTake a test on your terms and definitions
spaced repetition
Spaced RepetitionScientifically backed study method
heart puzzle
Matching GameHow quick can you match all your cards?
flashcards
FlashcardsStudy terms and definitions

1 / 5

6 Terms

1

Which of the following is a proper way to declare and initialize an int variable in Java?

int myNumber = 10;

New cards
2

Consider the following code snippet.

public static void main(String args[]){
final int z;
z = 20;
z = 30;
System.out.println(z);
}

What value of z is actually printed?

This code gives an error.

New cards
3

What are the memory values associated with the variables x, y, and z after the code snippet below executes?

int x = 7;            
double y = 2.0;
boolean z = false;

x = x + 3;
z = true;

x holds the int value 10, y holds the double value 2.0 and z holds the boolean value true.

New cards
4

Which of the following variable names follows best practices for naming a variable?

5apples

someVariable

applesnum

numApples

numApples

New cards
5

What does the keyword final do?

It prevents variables from being altered.

New cards
6

What will be the output of the following code snippet:

public class Variables 
{
public static void main(String[] args)
{
int totalBirds = 150;
double averageTime = 45.7;
String mostCommon = "Mallard Duck";

System.out.println("Bird Watching Results");
System.out.print("Total birds seen: ");
System.out.println(totalBirds);

System.out.print("Average time between sightings: ");
System.out.println(averageTime);

System.out.print("Most common bird seen was ");
System.out.println(mostCommon);
}
}

Bird Watching Results
Total birds seen: 150
Average time between sightings: 45.7
Most common bird seen was Mallard Duck

New cards
robot