Preview

06 - AP Exam question set #6 (Hard)

 1. Evaluate the following Java expression, if x=3, y=5, and z=10:
Expression: ++z + y - y + z + x++

  23

  25

  20

  24

 2. Whenever the first number is smaller than the second, the remainder is the second number.
System.out.println(2 % 3);

  FALSE

  TRUE

 3. The following code prints 135.
System.out.println("13" + 2 + 3);

  FALSE

  TRUE

 4. Assume that SomeClass and MainClass are properly defined in separate files. What is the output of main()?
class SomeClass
{
    int someVar;
}

public class MainClass
{
    public static void main(String[] args)
    {
        SomeClass x;
        System.out.println(x.someVar);
    }
}

  unknown value

  Answer: 0

  runtime error

  compile error

 5. The above code gives error that ____________________________ with a call to the SomeClass constructor.

  x has not been correctly defined as a type. It needs to be defined

  x has not been created as an object. It needs to be created

  x has not been initialized. It needs to be initialized

  x has not been instantiated. It needs to be instantiated

 6. Refer to the following code. If there were curly braces around the two indented statements, then:
int x = 3;
int y = 2;
if (y / x > 0)
   System.out.print("test ");
   System.out.print("track ");

  test will be printed

  nothing will be printed

  track will be printed

  test track will be printed

 7. The following code:
int findSmallest(int[] values) {
  int minIndex = 0;
  for (int i=1; i

  iterates over a non-empty array to find the index of the smallest element

  iterates over a non-empty array to find the index of the first indexed element

  iterates over a non-empty array to find the element contained in the first index

  iterates over an empty array to find the index of the largest element

 8. Given this code, which of the following will cause an infinite loop? Assume temp is an int variable initialized to be greater than zero and that a is an array of ints.
for ( int k = 0; k < a.length; k++ )
{
   while ( a[ k ] < temp )
   {
      a[ k ] *= 2;
   }
}

  The values don't matter this will always cause an infinite loop.

  Whenever a includes a value that is less than or equal to zero.

  Whenever a has values larger then temp.

  When all values in a are larger than temp.

 9. If nums has been created as an ArrayList object and initially contains integer values: [0, 0, 4, 2, 5, 0, 3, 0], what will nums contain as a result of executing method numQuest?
private List nums;

//precondition: nums.size() > 0
//nums contains Integer objects
public void numQuest() {
   int k = 0;
   Integer zero = new Integer(0);
   while (k < nums.size()) {
      if (nums.get(k).equals(zero))
         nums.remove(k);
      k++;
   }
}

   [3, 5, 2, 4, 0, 0, 0, 0]

  [4, 2, 5, 3]

  [0, 0, 4, 2, 5, 0, 3, 0]

   [0, 4, 2, 5, 3]

 10. What is the output of the following program?
public class testincr
{
public static void main(String args[])
{
int i = 0;
i = i++ + i;
System.out.println(?I = ? +i);
}
}

  I = 1

  I = 0

  I = 2

  I = 3

 11. What needs to go on line 7 in order for the code to print: 101 Adam
class Student{  
 int id;  
 String name;  
}  
class TestStudent3{  
 public static void main(String args[]){  
  _____________________________ 
  //Initializing objects  
  s1.id=101;  
  s1.name="Adam";  
  //Printing data  
  System.out.println(s1.id+" "+s1.name);   
 }  
}  

  Student s1=new Student();

  s1 = Student new student();

  Student s = new student();

  s1 = new Student(student);

 12. What is the output of the following program?
class recursion {
        int func (int n) {
            int result;
            if (n == 1)
                return 1;
            result = func (n - 1);
            return result;
        }
    } 
    class Output {
        public static void main(String args[]) {
            recursion obj = new recursion() ;
            System.out.print(obj.func(5));
        }
    }

  2

  120

  1

  None of the listed answers are correct

 13. The output of the following program is 120.
class recursion {
        int fact(int n) {
            int result;
            if (n == 1)
                return 1;
            result = fact(n - 1) * n;
            return result;
        }
    } 
    class Output {
        public static void main(String args[]) {
            recursion obj = new recursion() ;
            System.out.print(obj.fact(5));
        }
    }

  True

  False

 14. What is the output of the following program?
public class MyFirst {  
      public static void main(String[] args) {  
         MyFirst obj = new MyFirst(n);  
 }  
 static int a = 10;  
 static int n;  
 int b = 5;  
 int c;  
 public MyFirst(int m) {  
       System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);  
   }  
// Instance Block  
  {  
     b = 30;  
     n = 20;  
  }   
// Static Block  
  static   
{  
          a = 60;  
     }   
 }  

  10, 5, 0, 20, 0

  60, 30, 0, 20, 0

  60, 5, 0, 20

  10, 30, 20

 15. ABH8097 would be considered a valid long literal.

  FALSE

  TRUE