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

  integer

  mixable

  character

  variable

 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;

  3

  Both 1 and 2 are valid declarations

  4

  Both 5 and 6 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;

  variable data type

  string data type

  int data type

  interchangeable data type

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

  It represents a number that is over 100

  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

 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. 

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

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

  Never

  Yes, but only if it is an integer

 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 );
  }
}

  There are no variables in this code

  The last line

  Line 5: long payAmount = 123;

  Line 1: class Example

 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

  primordial

  primitive

  intuitive

  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

  methods

  classes

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

  short is not a primitive data type

  String is actually a class, 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.

  initial

  fractional

  bit

  separate

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

  FALSE

  TRUE

 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 is text only (short words)

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

  Yes. Data of type short can be in this range

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

 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

  mitigator

  instance

  federal

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

  32 bit int singles

  16 bit double literals

  64 bit int 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"

  double-precision literal

  class based exponent type

  double primitive type string

  single-precision literal

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

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

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

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

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

 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. 

  identifer

  char

  var

  strong

 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"

  TRUE

  FALSE

 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

  string literal

  var char

  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.

  All of them are valid rules for the naming of identifiers

  1,3, and 6 are not valid

  None of the are valid rules

  1,2 and 5 are not valid

 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

  specific word replacement value

  predefined meaning

  integer only value

 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

  float is a reserved word

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

 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 51 represents a Q in bytecode

  the Unicode value of Q is '\u0051'.

  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";

  variable

  data type

  object

  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-_______________

  Methods

  Literals

  Objects

  Parameters