Preview

04 - Variables and Data Types

 1. A _______ is a location in memory (storage area) to hold data.
variables_and_Datatypes_java_programming_v1.jpg

  mixable

  variable

  character

  integer

 2. To indicate the storage area, each variable should be given a unique name. This is also called an __________in Java.

 3. Which of the following depicts the correct way to declare variables in Java?
1>> id speedLimit = 80

2>> int speedLimit > 80;

3>> speedLimit = 80

4>> int speedLimit = 80;

5>> dim speedLimit = 80;

6>> var speedLimit = 80;

  4

  Both 5 and 6 are valid declarations

  3

  Both 1 and 2 are valid declarations

 4. In this example, maxSpeed is a variable of ____________, and is assigned value 80. Meaning, maxSpeed variable can store integer values.
int maxSpeed =30;

  string data type

  int data type

  variable data type

  interchangeable data type

 5. What does the following 16 bit pattern represent?
0000000000000000

  It represents a string of characters, e.g. a word

  Without knowing more information, it is impossible to say

  It represents a very small fractional number

  It represents a number that is over 100

 6. Is it necessary for a variable to always have a data type?
Read and remember this definition: variable ? a named location in main memory 
which uses a particular data type to hold a value.Also, note that a variable
cannot be used in a program unless it has been declared. 

  Never

  Yes, otherwise it would not be clear what its bits represent

  Yes, but only if it is an integer

  No, except for when the variable is a very large number

 7. One what line is a variable being declared in this snippet of code?
class Example
{
  public static void main ( String[] args )
  {
    long payAmount = 123; 

    System.out.println("The variable contains: " + payAmount );
  }
}

  Line 5: long payAmount = 123;

  There are no variables in this code

  Line 1: class Example

  The last line

 8. When we use data we do not have to invent our own scheme to represent it with bits. The list below shows the ___________ data types.
There are types of data that are so fundamental that ways to 
represent them are built into Java

The list below shows ___________ Data Types
=======================

byte
short
int
long
float
double
char
boolean

  intuitive

  primitive

  primordial

  instinctive

 9. Upper and lower case characters are important in these names (referring to the list above). So "byte" is the name of a data type, but "BYTE" is not
Similarly, "Int" is not valid, but "int" is.

  TRUE

  FALSE

 10. All data in Java falls into one of two categories: primitive data and _______.

  objects

  properties

  classes

  methods

 11. Which one of the items on the list is incorrect?
int - primitive
String - primitive
double - primitive
Scanner - class
short - primitive

  String is actually a class, not a primitive data type

  short is not a primitive data type

  They are all correct

  Scanner is a primitive data type, not a class

 12. Integer types have no_______ part; floating point types have a _________ part.

  fractional

  separate

  initial

  bit

 13. All values represented using the short data type use 64 bits. All values represented using the long data type use 16 bits.

  TRUE

  FALSE

 14. Would data type short be an appropriate choice for dealing with the number 1,723,774 in your computer program?

  No. Data of type short can be only in the range -32,768 to +32,767.

  Yes. Data of type short would be valid up to 8 billion.

  No. Data of type short is text only (short words)

  Yes. Data of type short can be in this range

 15. When you write a program you do not have to know how to represent a number in bits. You can type the number just as you would on a typewriter. This is called a ______.

  literal

  instance

  mitigator

  federal

 16. All the below examples are:
125  
-32  
16  
0  
-123987

  64 bit int literals

  32 bit int singles

  16 bit double literals

  32 bit int literals.

 17. The following is an integer literal. 197.01

  TRUE

  FALSE

 18. 81E-06 is an example of a _________________. The big E means "times 10 to the power of"

  single-precision literal

  class based exponent type

  double-precision literal

  double primitive type string

 19. What is wrong with this number, considered to be of float type? 1230.00089F

  There are twoo many integers in it (max number of integers =3)

  There are nine decimal places of precision. Data type float can't handle that. The compiler will round the number

  There is nothing wrong with the number - it can be perfectly represented

  There is an 'F' in it, which is not allowed

 20. Computer programs frequently work with character data. The primitive data type for characters in Java is named _____.
The ____ type represents a character using 16 bits. 
In many programming languages, only 8 bits are used for this purpose. 
Java uses 16 bits so that a very large number of characters can be 
represented, nearly all of the characters in all of the World's languages.
 The method used is called Unicode. 

  var

  strong

  identifer

  char

 21. Is the following statement, with example, true or false?
A character literal is a single character with an apostrophe on each side: 
'm'       'y'         'A'
A control character is represented with a special sequence of characters: 
'\n'       '\t'

  FALSE

  TRUE

 22. This is a String, which is not primitive data. It is an object. Strings are surrounded by double quote marks ", not by apostrophes.
"testandtrack"

  FALSE

  TRUE

 23. Another of the primitive data types is the type_______. It is used to represent a single true/false value. A __________value can have only one of two values.
Additional important note:
==========================
There are two types of variables in Java, 
>>primitive and 
>>reference type.

All the basic types e.g. int, boolean, char, short, float, long and 
double are known as primitive types. JVM treats them differently 
than reference types, which is used to point objects e.g. 
String, Thread, File and others.

  min int

  var char

  string literal

  boolean

 24. What is printed to the screen?
class Example
{
  public static void main ( String[] args )
  {
    long pAmount = 777;    //the declaration of the variable

    System.out.println("The variable contains: " + pAmount );
  }
}

  The variable contains 777

  System.out.println(777)

  Error

  pAmount=777

 25. Which of the following are NOT valid rules for identifiers?
Use only the characters 'a' through 'z', 'A' through 'Z', 
Use only '0' through '9', character '_', and character '$'.
An identifier can not contain the space character.
Do not start with a digit.
An identifier can be any length.
Upper and lower case count as different characters. 
SUM  and  Sum  are different identifiers.
An identifier can not be a reserved word.
An identifier must not already be in use in this part of the program.

  1,2 and 5 are not valid

  1,3, and 6 are not valid

  All of them are valid rules for the naming of identifiers

  None of the are valid rules

 26. A reserved word is a word which has a _________________ in Java. For example int, double, true, and import are reserved words

  replacement value of a dot or star

  integer only value

  specific word replacement value

  predefined meaning

 27. What is wrong with the last item on the list (if anything)?
int myPay, yourPay; // OK
long good-by ;  // bad identifier: "-" not allowed
short shrift = 0;  // OK
double bubble = 0, toil= 9, trouble = 8 // missing ";" at end.
byte the bullet ;    // bad identifier: can't contain a space
int  double;    // bad identifier: double is a reserved word
char thisMustBeTooLong  ;   // OK in syntax, but a poor choice 
                            // for a variable name
int  8ball;    // bad identifier: can't start with a digit
float a=12.3; b=67.5; c= -45.44; 

  Nothing is wrong

  a is a reserved word

  bad syntax: don't use ";" to separate variables

  float is a reserved word

 28. When you run the following program, you get the output 'Q' because:
class CharExample {
    public static void main(String[] args) {
    	
    	char letter = '\u0051';
    	System.out.println(letter);
    }
}

  the Unicode value of Q is '\u0051'.

  the 51 represents a Q in bytecode

  the first letter of the slash is a U and + 5 = Q

  the \ represents a Q in ASCII

 29. Java also provides support for character strings via java.lang.String class. Here's how you can create a String ______in Java.
myString = "Thanks, www.testandtrack.io";

  data type

  object

  variable

  literal

 30. Fill in the blanks for the fourth type of variable in Java.
There are 4 types of variables in Java:
=======================================
1-Instance Variables (Non-Static Fields)
2-Class Variables (Static Fields)
3-Local Variables
4-_______________

  Parameters

  Literals

  Objects

  Methods