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 



No comments:

Post a Comment