Showing posts with label java+question. Show all posts
Showing posts with label java+question. Show all posts

Saturday, October 22, 2016

Java - Test Yourself.

 • Which of the following is an invalid list of elements in an array?
 "Joe", "Doug", "Anne"
 "Joe", 3, 25.5
 3, 17, 12
 12.3, 32.6, 21.7

 • What happens when the code that follows is executed?
 
   int[] nums = new int[4];
   for (int i = 0; i <= nums.length; i++)
   {
       nums[i] = i;
   }
   System.out.println(nums[2]);

 It prints “1” to the console.
 It prints “2” to the console.
 It prints “3” to the console.
It throws an ArrayIndexOutOfBoundsException.


 • What is printed to the console when the code that follows is executed?
 
   int[] years = new int[5];
   years[1] = 1992;
   for (int i = 0; i < years.length; i++)
   {
       years[i]++;
   }
   System.out.println(years[3]);

 “3”
 “4”
 “1”
 “1994”

 • Before you can sort an array of objects created from a class that you 
   defined, the class must implement the ____________ interface.

   (Comparable)

 • Which of the following, if any, is an invalid array declaration?

 String[] names = new String[5];
 String names[] = new String[5];
 String[] names = new String[0];
 String[] names = {"one", "two"};
 all are valid


 • How many rows are in the array that follows?
 
   Rental[][] transactions = new Rental[7][3];

 7
 6
 3
 2

 • What is the value of temps[2][1] after the code that follows is executed?
 
   double[][] temps = new double[10][5];
   for (int i = 0; i < temps.length; i++)
   {
       for (int j = 0; j < temps[i].length; j++)
       {
           temps[i][j] = j + i;
       }
   }
 1.0
 2.0
 3.0
 4.0


 • A two-dimensional array whose rows can have different numbers of 
   columns is called a ____________ array. 

   (jagged)


 • What is the value of the variable named len after the code that follows is executed?
 
   int[][] nums = { {1, 2, 3}, {3, 4, 5, 6, 8}, {1}, {8, 8} };
   int len = nums.length;

 1
 2
 3
 4
 5


 • What is the value of the third element in the array that follows?
 
   double[] percents = new double[4];
   percents[1] = 85.66;
   percents[2] = 56.98;
   percents[3] = 25.66;

 25.66
 56.98
 85.66
 a third element doesn’t exist


 • What is printed to the console when the code that follows is executed?
 
   int[][] points = { {8,3}, {4,3}, {7,2} };
   String s = "";
   for (int i = 0; i < points.length; i++)
   {
       Arrays.sort(points[i]);
       for (int j = 0; j < points[i].length; j++)
       {
           s += points[i][j];
       }
   }
   System.out.println(s);

 383427
 437283
 342738
 233478


 • What is the value of the string named lowest when the code that follows is executed?
 
   String[] types = {"lux", "eco", "comp", "mid"};
   Arrays.sort(types);
   String lowest = types[0];

 lux
 eco
 comp
 mid


 • What is the highest index value associated with the array that follows?
 
   byte[] values = new byte[x];

 0
 x
 x + 1
 x – 1
 can’t tell from information given


 • Consider the following code:
 
   double[] times = {3.56, 3.9, 2.6, 4.5, 2.4, 5.2};
   double[] bestTimes = new double[4];
   Arrays.sort(times);
   bestTimes = Arrays.copyOfRange(times, 1, 4);
 
 • What values are in the bestTimes array?

 2.4, 2.6, 3.56
 2.6, 3.56, 3.9
 2.4, 2.6, 3.56, 3.9
 2.6, 3.56, 3.9, 4.5


 • What does the x represent in the following declaration?
 
   BigDecimal[][] sales = new BigDecimal[x][y];

 the number of elements in each array
 the number of arrays in the sales array
 the number of tables in the sales array


 • What is the value of the variable named lists after the statements that follow are executed?
 
   String[][] names = new String[200][10];
   int lists = names.length;

 9
 10
 199
 200
 code doesn’t compile

 • A two-dimensional array whose rows all have the same number of 
   columns is called a ____________ array.  

   (rectangular)


 • What is printed to the console when the code that follows is executed?
 
   int[] values = {2, 1, 6, 5, 3};
   Arrays.sort(values);
   int index = Arrays.binarySearch(values, 5);
   System.out.println(values[index] - 1);

 “2”
 “3”
 “4”
 “5”
 “6”


 • Which of the following statements performs the same task as the 
   for loop in the code that follows?
 
   double[] times = new double[3];
   for (int i = 0; i < times.length; i++)
   {
       times[i] = 2.0;
   }

 Arrays.fill(times, 0, 3, 2.0);
 Arrays.fill(times, 1, 3, 2.0);
 Arrays.fill(times, 1, 4, 2.0);
 Arrays.fill(times, 0, 2, 2.0);


 • What is printed to the console after the code that follows is executed?
 
   int[][] types = new int[4][];
   for (int i = 0; i < types.length; i++)
   {
       types[i] = new int[i+1];
   }
   System.out.println(types[1].length);

 “2”
 “4”
 “1”
 “3”
 code doesn’t compile


 • Which of the following is an invalid two-dimensional array definition?

 double[][] values = new double[2][8];
 double values[][] = new double[8][2];
 double[][] values = new double[8][];
 double[][] values = new double[][8];


 • What is printed to the console when the code that follows is executed?
 
   int[] nums = new int[2];
   int[] vals = new int[2];
   if (Arrays.equals(nums, vals))
       System.out.println("One");
   vals[1] = 2;
   nums = vals;
   if (Arrays.equals(nums, vals))
       System.out.println("Two");

 “One”
 “Two”
 “One” and “Two”
 nothing is printed to the console


 • What is the value of names[4] in the following array?
 
   String[] names = {"Jeff", "Dan", "Sally", "Jill", "Allie"};

 Sally
 Jill
 Allie
 name[4] doesn’t exist


 • An enhanced for loop can only be used

 to work with arrays that contain integers
 to work with one-dimensional arrays
 to work with all the elements of an array
 to work with jagged arrays


 • To use the binarySearch method of the Arrays class on an array of ints, 
   you must first

 create an Arrays object
 sort the array
 implement the Comparable interface
 override the equals method


 • What is the value of grades[1] after the following statements are executed?
 
   int[] grades = new int[2];
   grades[0] = 98;
   grades[1] = 84;
   grades = new int[2];

 98
 84
 0

 • What is the value of times[2][1] in the array that follows?
 
   double[][] times = { {23.0, 3.5}, {22.4, 3.6}, {21.3, 3.7} };

 3.7
 22.4
 3.5
 21.3
 3.6


 • Why won’t the following code execute?
 
   double values[] = new double[5];
   values[5] = 6;

 The brackets in the first statement are misplaced.
 An int value can’t be assigned to a double.
 The index is out of bounds.


 • Since the sort method of the Arrays class can sort String objects, what must be true of the String class?

 It contains a sort method.
 It implements the comparable method.
 It inherits a sort method.
 It implements the Comparable interface.


 • Which of the following statements gets the number of Customer objects 
   in the array that follows?
 
   Customer[] customers = new Customer[55];

 int size = customers.length();
 int size = customers.length;
 int size = customers.size();
 int size = Arrays.size(customers);


 • When you use an enhanced for loop with an array, you don’t 
   need to use a ________________ to iterate through the elements of the array.

   (counter variable or counter) 


 • Before you can use the binarySearch method of the Arrays class to 
   search for an element with a specified value in an array, you must ________________.  
   (sort the array)

 • Each row of a two-dimensional array is stored as a/an ____________. 
   (array)

 • Consider the following code:
 
   double[] times = {3.56, 3.9, 2.6, 4.5, 2.4, 5.2}; 
   double[] bestTimes = new double[4]; 
   Arrays.sort(times); 
   bestTimes = Arrays.copyOfRange(times, 1, 4); 
 
   What is the length of the bestTimes array?

 6
 5
 4
 3


 • What is the value of the variable named len after the code that follows is executed?
 
   int[][] nums = { {1, 2, 3}, {3, 4, 5, 6, 8}, {1}, {8, 8} };
   int len = nums[2].length;

 1
 2
 3
 4
 5


 • Consider the following code:
 
   double[] times = {3.56, 3.9, 2.6, 4.5, 2.4, 5.2};
   double[] bestTimes = new double[4];
   Arrays.sort(times);
   bestTimes = Arrays.copyOfRange(times, 1, 4);
 
   What values are in the times array after the code is executed?

 2.4, 2.6, 3.56, 3.9, 4.5, 5.2
 3.56, 3.9, 2.6, 4.5, 2.4, 5.2
 2.4, 2.6, 3.56
 2.4, 2.6, 3.56, 3.9


 • What is the value of nums[2] after the following statements are executed?
 
   int[] values = {2, 3, 5, 7, 9};
   int[] nums = values;
   values[2] = 1;

 3
 1
 5
 0






Saturday, September 24, 2016

JAVA Questions.


  • What is a compiler?
  • How is Java different from traditional programming?
  • What is bytecode?
  • What is the difference between JRE and JDK?
  • If you had saved the code in a file named "whatever.java", would it have compiled without errors?
  • If you had used a file extension other than java when saving the code, would it have compiled?
  • In Java, how do you write to the console?
  • Write a Java class named HelloWorld that prints "Hello World!".


  • What does ASCII stand for?
  • Does Java use ASCII or Unicode?
  • What are reference type variables?
  • What are primitive type variables?
  • How are constants implemented in Java?
  • What is an expression?
  • How to assign the British pound symbol to a "char" if you know the Unicode for it is 00A3?
  • Name ten operators in Java.
  • What is the ternary operator in Java?
  • How to specify comments in Java?
  • What is operator precedence?
  • Consider the following code. What are the values of result 1 and result2? Why the difference?
int result1 = 1 + 2 * 3;
int result2 = (1 + 2) * 3;


  • Name at least three element types that a class can contain. 
  • What are the differences between a method and a constructor? 
  • Does a class in a class diagram display its constructors? 
  • What does null mean? 
  • What do you use the this keyword for? 
  • When you use the == operator to compare two object references, do you actually compare the contents of the objects? Why? 
  • What is variable scope? 
  • What does “out of scope” mean? 
  • How does the garbage collector decide which objects to destroy? 
  • What is method overloading? 
  • Create a class whose fully-qualified name is com.example.Tablet to model an Android tablet. The class must have three private fields, weight (int), screenSize (float), and wifiOnly (boolean). Access to the fields must be through pairs of public get and set methods. That is getWeight/setWeight, getScreenSize/setScreenSize and getWifiOnly/setWifiOnly. The class must also have one constructor, a no-argument constructor.
  • Create a TabletTest class in the package com.example.test and instantiate the Tablet class. Print the value of the fields (by calling its get methods) right after instantiation. Then, set the field values and print them again

Bytecodes are
      1. output from the JVM
      2. ready to be executed by a Windows sytem
      3. input to the Java compiler 
      4. input to the Java interpreter (JVM) 

Which of the following is a valid statement for declaring and 
initializing a double variable named length to a starting value of 120?
      1. Double length = 120.0; 
      2. length = 120.0; 
      3. double length = 120.0; 

Which of the following can you assign to a String variable?
      1. a string literal (constant) 
      2. null 
      3. an empty string 
      4. all of the above 

Relational equality operator is allowed to
      1. compare numeric variables 
      2. compare string references 
      3. both a and b 
      4. neither a nor b 

What happens when you use both integer and double values in an 
arithmetic expression?
      1. an exception occurs 
      2. all values are cast to the type of the result variable 
      3. the integer values are cast to double values 
      4. the double values are cast to integer values 

What does the following statement do if userNumber has an integer 
value of 14 and userEntry has a string value of “two”?

  userNumber = Integer.parseInt(userEntry);

      1. Stores a null value in userNumber 
      2. Converts “two” to 2 and stores it in userNumber 
      3. Throws an exception 
      4. Converts “two” to “2” and stores it in userEntry 

Explicit casts must be used to perform
      1. narrowing conversions 
      2. widening conversions 
      3. none of the above 
      4. any conversion between two data types 

Consider the following condition. It will be true when
(!endProgram.equalsIgnoreCase("n"))

      1. the value of endProgram doesn’t equal “n” 
      2. the value of end Program equals “n” or “N” 
      3. the value of endProgram equals “n” 
      4. the value of endProgram doesn’t equal “n” or “N” 

Consider the code that follows. What does it do?

   String value = "2";
   boolean tryAgain = true;
   while (tryAgain == true) {

     try {
        int num = Integer.parseInt(value);
        tryAgain = false;
     }
     System.out.println("Valid integer");
     catch(NumberFormatException nfe) {
        System.out.println("Invalid integer");
        System.out.print("Enter an integer");
     }
   }

      1. The code compiles but causes a runtime error. 
      2. It prints “Invalid integer” to the console. 
      3. The code doesn’t compile. 
      4. It prints “Valid integer” to the console. 

If the variable named input is 755, what is the value of r 
after these statements are executed?
 
   NumberFormat n = NumberFormat.getNumberInstance();
   n.setMinimumFractionDigits(3);
   String r = n.format(input);
      1. 755 
      2. .755 
      3. 760.000 
      4. 755.000