Java Primitive Data Types and Variables
80 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the data type of the following literal? "fooled you"

String

What is the camel case variable name for a variable that represents the top score?

topScore

What are the three primitive data types that will be tested on the AP Exam?

  • String char double
  • double boolean int (correct)
  • char int String
  • int double boolean
  • What is the camel case variable name for a variable that represents a shoe size?

    <p>shoeSize</p> Signup and view all the answers

    What data type should you use to represent the average grade for a course?

    <p>double</p> Signup and view all the answers

    What data type should you use for a shoe size like 8.5?

    <p>double</p> Signup and view all the answers

    What data type should you use to record if it is raining or not?

    <p>boolean</p> Signup and view all the answers

    What is the data type of the following literal? 'A'

    <p>char</p> Signup and view all the answers

    What data type holds true/false values?

    <p>boolean</p> Signup and view all the answers

    What data type should you use to represent the amount of money in a bank account?

    <p>double</p> Signup and view all the answers

    Which of the following is NOT the name of a Java primitive data type?

    <p>String</p> Signup and view all the answers

    What are the two varieties of data in Java?

    <p>objects and primitive</p> Signup and view all the answers

    Which of the following is the correct header line for the main method in Java?

    <p>public static void main (String args [])</p> Signup and view all the answers

    A ______ is a note written to a human reader of a program.

    <p>comment</p> Signup and view all the answers

    Which of the following will result in an error?

    <p>12 + (13 + 7)/2 ) * 4</p> Signup and view all the answers

    What are the two steps that take place when an assignment statement is executed?

    <p>(i) Evaluate the Expression, and (ii) Store the value in the variable.</p> Signup and view all the answers

    On a single line of code declare x, y, and z all to be an integer data type.

    <p>int x, y, z;</p> Signup and view all the answers

    (2 - 6) / 2 + 9

    <p>7</p> Signup and view all the answers

    Which of the following is legal?

    <p>x = 9</p> Signup and view all the answers

    State what is printed. int x = 40; int y = 4; System.out.println(2 + 8 * y / 2 - x);

    <p>-22</p> Signup and view all the answers

    What is the value of the expression: -5 * 9 / 10 + 10 * 5 / 9

    <p>1</p> Signup and view all the answers

    On a single line of code declare x, y, and z to be double and on that same line initialize them all to be 3.14.

    <p>double x = 3.14, y = 3.14, z = 3.14;</p> Signup and view all the answers

    State what is printed. int a = 100; int b = 200; b/=a; System.out.println(b + 1);

    <p>3</p> Signup and view all the answers

    What is another way to write p = p - 1;?

    <p>p--;</p> Signup and view all the answers

    The following code stores a 20 in the variable num; double num = 61/3; What small change can you make to this single line of code to make it produce the "real" answer to the division?

    <p>double num = (double) 61/3;</p> Signup and view all the answers

    State what is printed. System.out.println((double)(90/9));

    <p>10.0</p> Signup and view all the answers

    Write code that will calculate and print the square root of 435.61.

    <p>System.out.println(Math.sqrt(435.61));</p> Signup and view all the answers

    Which of the following would return a random number from 1 to 6 inclusive?

    <p>(int)(Math.random()*6)+1</p> Signup and view all the answers

    What is the purpose of wrapper classes?

    <p>to convert primitive type variables into objects containing the equivalent information.</p> Signup and view all the answers

    Write a Java formula using the Math class for: Distance = |val1 - val2|

    <p>Math.abs(val1-val2)</p> Signup and view all the answers

    Evaluate: Math.ceil(115.8)

    <p>116.0</p> Signup and view all the answers

    Evaluate: Math.abs(2 + -4)

    <p>2</p> Signup and view all the answers

    Evaluate: Math.sqrt(16) * Math.max(Math.abs(-5), Math.abs(-3))

    <p>20.0</p> Signup and view all the answers

    The classes that convert primitives to objects are called _____ classes

    <p>wrapper</p> Signup and view all the answers

    Evaluate: Math.min( 8, 3 + 2 )

    <p>5</p> Signup and view all the answers

    Write a formula that will find the distance between two values, num1 and num2 by taking the absolute value of their difference. Assign the answer to double x.

    <p>double x = Math.abs(num1-num2)</p> Signup and view all the answers

    Evaluate: Math.sqrt(76 + 45)

    <p>11.0</p> Signup and view all the answers

    Which of the following would return a random int from 1 to 10 inclusive?

    <p>(int)(Math.random()*10)+1</p> Signup and view all the answers

    What is the value of len after the following executes? String s1 = "Love you!"; int len = s1.length();

    <p>9</p> Signup and view all the answers

    What is the value of str2 after the following code executes? String s1 = "helicopter"; String s2 = s1.substring(4, 7);

    <p>cop</p> Signup and view all the answers

    Consider the following code segment. String s1 = "Queen Mary"; String s2 = "1936 Queen Mary"; String s3 = "Queen Elizabeth"; System.out.print(s2.indexOf(s1) + " "); System.out.println(s1.indexOf(s3)); What will be output when the code segment executes?

    <p>5 -1</p> Signup and view all the answers

    What will be the value of r? String r = "Saturday Night Live"; r = r.substring(5, 8);

    <p>day</p> Signup and view all the answers

    Consider the following code segment. String s1 = "Djakarta"; String s2 = s1.substring(3, 7); String s3 =s1.substring(3); System.out.println(s2); System.out.println(s3); What will be output when the code segment executes?

    <p>kart karta</p> Signup and view all the answers

    Consider the following code segment. String test = "This is a test."; System.out.println(test.indexOf("is")); What is printed as a result of executing the code segment?

    <p>2</p> Signup and view all the answers

    What is the output from the following code? String s = "Computer Science is fun!"; String s1 = s.substring(0, 8); String s2 = s1.substring(1); String s3 = s2.substring(1, 3); System.out.println(s3);

    <p>mp</p> Signup and view all the answers

    Consider the following code segment. String str = "Hello World!"; System.out.println(str.length()); What is printed as a result of executing the code segment?

    <p>12</p> Signup and view all the answers

    Consider the following code segment. String s1 = "A"; String s2 = "H"; System.out.print(s1.compareTo("B")); System.out.print(" "); System.out.println(s2.compareTo("B")); What is printed as a result of executing the code segment?

    <p>-1 6</p> Signup and view all the answers

    Write a series of statements which acquires the length of name and stores it in a variable named len. You must first declare len. String name = "SpongeBob SquarePants";

    <p>int len; len = name.length();</p> Signup and view all the answers

    Examine this code: String str = "Hello World!" ; What is the index of the character 'e' ?

    <p>1</p> Signup and view all the answers

    What sort of thing is "Happy Days" in the following: String str = "Happy Days";

    <p>A String literal</p> Signup and view all the answers

    What is returned by a method of type void?

    <p>nothing</p> Signup and view all the answers

    New objects can be created by using the new operator with an appropriate constructor.

    <p>True</p> Signup and view all the answers

    What is the relationship between an object, method, and a class.

    <p>Objects are defined in a class and methods perform a task.</p> Signup and view all the answers

    Which of the following is equivalent to !(a && (c < d))

    <p>!a || (c &gt;= d)</p> Signup and view all the answers

    Consider the following declarations. int valueOne, valueTwo; Assume that valueOne and valueTwo have been initialized. Which of the following evaluates to true if valueOne and valueTwo contain the same value?

    <p>valueOne == valueTwo</p> Signup and view all the answers

    The boolean expression !(A || B) evaluates to

    <p>true whenever both A is false and B is false.</p> Signup and view all the answers

    State what's printed: int a = 84, a= 1; boolean tf = true; System.out.println(tf && !tf);

    <p>false</p> Signup and view all the answers

    Consider the following code segment which is intended to assign true to between if x is between lower and upper, inclusive, and false otherwise. Assume lower = lower) && (x = 90)isPassing = true;else if (testAverage >= 75 && assignmentsCompleted >= 4)isPassing = true;elseisPassing = false;II. boolean pass = false;if (testAverage >= 90)pass = true;else if (testAverage >= 75 && assignmentsCompleted >= 4)pass = true;isPassing = pass;III. isPassing = (testAverage >= 90) || (testAverage >= 75 && assignmentsCompleted >= 4);Which of the implementations will work correctly assuming that the variables have been initialized?

    <p>I, II, and III</p> Signup and view all the answers

    State what's printed: int a = 84, b = 1; boolean tf = true; System.out.println((a != 1) || tf || (a == b));

    <p>true</p> Signup and view all the answers

    What is output by the following code? int a = 90, b = 8; if( (ab) ) a = b; System.out.println(b++);

    <p>8</p> Signup and view all the answers

    A teacher put three bonus questions on a test and awarded 5 extra points to anyone who answered all bonus questions correctly and no extra points otherwise. Assume that the boolean variables bonusOne, bonusTwo, and bonusThree indicate whether a student has answered the particular question correctly. Each variable was assigned true if the answer was correct and false if the answer was incorrect. Which of the following code segments will properly update the variable grade based on a student's performance on the bonus questions? I. if (bonusOne && bonusTwo && bonusThree) grade += 5; II. if (bonusOne || bonusTwo || bonusThree) grade += 5; III. if (bonusOne) grade += 5; if (bonusTwo) grade += 5; if (bonusThree) grade +=5;

    <p>I only</p> Signup and view all the answers

    Consider the following code segment. int x = ; int y = ; boolean result = (x < y); result = ( (x >= y) && !result ); Which of the following best describes the conditions under which the value of result will be true after the code segment is executed?

    <p>Only when x &gt;= y</p> Signup and view all the answers

    The boolean expression (A || B) && (!A || !B) evaluates to

    <p>true whenever only A is true or only B is true.</p> Signup and view all the answers

    Consider the following code segment. Assume value is an int. if (value < 0) || value < 100)System.out.print("Not in range"); elseSystem.out.print("In range"); Consider the following code segments that could be used to replace the code. I. if (value < 0){if (value > 100)System.out.print("Not in range"); elseSystem.out.print("In range");}elseSystem.out.print("In range");II. if (value < 0)System.out.print("Not in range"); else if (value < 100)System.out.print("Not in range");elseSystem.out.print("In range");III. if (value >= 0)System.out.print("In range"); else if (value b && b > c) if (b < a) b = -b; else a = -a;else if (a > b || b > c) if (a < c) c = -c; else if (! (c > b)) b = -a;Which of the implementations will work correctly assuming that the variables have been initialized?

    <p>I, II, and III</p> Signup and view all the answers

    Consider the following code. if (value < 0 || value > 100)System.out.println("true");elseSystem.out.println("false"); Given that value is an integer, which of the following code segments would have the same output as the code above?

    <p>I and II</p> Signup and view all the answers

    Consider the following code segment. int result= 99; if (num1 == num2) {result = 0:} else if (num1 > num2) {result = 1;} else {result = -1;} System.out.println(result); Which of the following code segments will print the same result as the code above no matter what integers are stored in num1 and num2?

    <p>I, II, and III</p> Signup and view all the answers

    Consider the following code segment. int x = int n = 100;if (x < 1000){ if (x > 1000) n = 200; else n = 300;}else{ if (x < 1000) n = 400; else n = 300;}System.out.println(n);What is printed as a result of executing the code segment?

    <p>300</p> Signup and view all the answers

    Show the basic skeleton of an if - else structure.

    <p>if( ) { } else { }</p> Signup and view all the answers

    What is output by the following? int x = 8, y = 5; if( !(x == y) ) { System.out.println("Sunday, Monday");} else { System.out.println("Happy Days!");}

    <p>Sunday, Monday</p> Signup and view all the answers

    Write a single statement that will store a true in the boolean variable tf only if boo is negative.

    <p>boolean tf = boo &lt; 0;</p> Signup and view all the answers

    Determine the output of the following code segments. Write no output if there is no output. int i = 2; int j = 3; if (j + 9 == i) { System.out.println ("You got it!");}

    <p>no output</p> Signup and view all the answers

    State what is printed: int a = 84, b = 1; boolean boo = a != b; System.out.println(boo);

    <p>True</p> Signup and view all the answers

    Determine the output of the following code segments. Write no output if there is no output. int i = 2; int j = 3; if (i > j) { System.out.println ("You got it!");}

    <p>no output</p> Signup and view all the answers

    How many different values can a boolean variable hold?

    <p>2</p> Signup and view all the answers

    The ________ method is where a program starts running.

    <p>main</p> Signup and view all the answers

    Create the heading for class called Skeleton.

    <p>public class Skeleton{}</p> Signup and view all the answers

    Consider the following code: System.out.print("Fire"); System.out.println(" Ants"); Which of the following is actually printed?

    <p>Fire Ants</p> Signup and view all the answers

    Which of the following is the most acceptable way of naming a variable?

    <p>groovyDude</p> Signup and view all the answers

    Which of the following is a keyword?

    <p>All of the above</p> Signup and view all the answers

    What data type would you use to store this number? 189.24

    <p>double</p> Signup and view all the answers

    Study Notes

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    This quiz covers essential concepts of primitive data types in Java, including int, double, boolean, and char. Additionally, it discusses variable naming conventions and key components such as the main method header and comments. Test your understanding of these foundational concepts in Java programming.

    More Like This

    Java Data Types and Variables Quiz
    28 questions
    Java Programming: Strings and Variables
    18 questions
    Java Primitive Data Types Quiz
    5 questions
    Use Quizgecko on...
    Browser
    Browser