Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java Fundamental questions

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Fundamental questions

    I found some good questins from BrainBench. It would be great if you could discuss and answer solutions of these questions.




    Sample Code
    synchronized void move() throws java.io.IOException {
    Additional code here
    }
    Referring to the sample code above, how can a method in the same class invoke move()?
    Choice 1 By calling wait() to obtain the object's monitor before calling move().
    Choice 2 By enclosing the call inside a "catch (java.io.IOException e)" block.
    Choice 3 By following the call with a "catch (java.io.IOException e)" block.
    Choice 4 By enclosing the call inside a "try" block.
    Choice 5 By enclosing the call inside a "try" block followed by a "catch (java.io.IOException e)" block.

    Text
    java com.brainbench.TestCommandLine -p Parameter1 Parameter2 Parameter3 Parameter4
    If the user enters the command line shown above, how many elements are contained in the array which is passed to "public static void main(String args[])" in TestCommandLine?
    Choice 1 Four
    Choice 2 Five
    Choice 3 Six
    Choice 4 Seven
    Choice 5 Nine

    Text
    java com.brainbench.TestCommandLine -p Parameter1 Parameter2 Parameter3 Parameter4
    If the user enters the command line shown above, how many elements are contained in the array which is passed to "public static void main(String args[])" in TestCommandLine?
    Choice 1 Four
    Choice 2 Five
    Choice 3 Six
    Choice 4 Seven
    Choice 5 Nine
    What is the command used to archive Foo.class and Bar.class into a jar file?
    Choice 1 zip myclasses.jar Foo.class Bar.class
    Choice 2 jar cvf myclasses.jar Foo.class Bar.class
    Choice 3 gzip myclasses.jar Foo.class Bar.class
    Choice 4 mkjar myclasses.jar Foo.class Bar.class
    Choice 5 winzip myclasses.jar Foo.class Bar.class

    Sample Code
    int a = 3; int b = 0;
    switch (a) {
    case 1: b = a + 2;
    case 2: b = a + 3;
    case 3: b = a + 4;
    case 4: b = a + 5;
    case 5: b = a + 6;
    default:
    b = a * 2;
    }
    // Line A
    What is the value of "b" when the execution of the above code reaches Line A?
    Choice 1 0
    Choice 2 6
    Choice 3 7
    Choice 4 8
    Choice 5 14
    What happens if you try to write to the twenty-first element of an array that contains twenty elements?
    Choice 1 The array expands automatically.
    Choice 2 The virtual machine presents an error dialog box.
    Choice 3 The attempted access throws an exception.
    Choice 4 The virtual machine terminates the program.
    Choice 5 The memory location just above the array is corrupted.
    For which one of the samples of code below does the cast operation result in a loss of information?
    Choice 1 long x; double y = (double) x;
    Choice 2 char x; int y = (char) x;
    Choice 3 double x; long y = (long) x;
    Choice 4 int x; double y = (double) x;
    Choice 5 int x; float y = (float) x;

    ---------------------------------------------------------------------------------------------------------------------------------------
    Code
    int a = 5, b = 7;
    int c = a += 2 * 3 + b--;
    What is the value of "c" after executing the code segment above?
    Choice 1 12
    Choice 2 18
    Choice 3 23
    Choice 4 28
    Choice 5 63
    public class Test {
    public static void main(String[] args) {
    if (args[0].equals("b") && checkFoo(args)) {
    System.out.println("if evaluated to true.");
    }

    System.out.println("x");
    }
    private static boolean checkFoo(String[] args) {
    System.out.println("Checking..");

    return args.length > 0;
    }
    }
    What is the result of running the above code sample with the following command "java Test a e"?
    Choice 1 Checking.. X
    Choice 2 if evaluated to true.
    Checking..X
    Choice 3 if evaluated to true. X
    Choice 4 It does not compile.
    Choice 5 x

    a. Scrollable is an interface.
    b. Writable is an interface.
    c. TextScreen is an abstract class.
    d. VideoScreen is a class that extends TextScreen and implements both Scrollable and Writable.
    Referring to the scenario above, which one of the following statements is always true?
    Choice 1 A VideoScreen cannot be passed to a method that expects a TextScreen.
    Choice 2 A VideoScreen can be cast to a Scrollable.
    Choice 3 A VideoScreen cannot be passed to a method that expects a Writable.
    Choice 4 A VideoScreen cannot be passed to a method that expects a Scrollable.
    Choice 5 A Scrollable can be cast to a VideoScreen.


    // This code segment should print out the numbers 1 to 20.
    int a = 1;
    // Line A
    System.out.println(a);
    a++;
    } while (a <= 20);
    In the code segment above, what is the correct syntax for Line A?
    Choice 1 {
    Choice 2 repeat {
    Choice 3 do {
    Choice 4 for(a = 1; a <= 20; a++) {
    Choice 5 loop {
    Code
    int i = 6;
    int j = 2;
    System.out.println(Integer.toString(i++ * 6 + 3 - 4 + --j));
    What does the fragment of code above print out?
    Choice 1 36
    Choice 2 42
    Choice 3 43
    Choice 4 44
    Choice 5 48

    When using an ArrayList as the implementation for a list collection, what happens if adding an element exceeds the ArrayList's capacity?
    Choice 1 The add() call returns with a value of -1, rather than the index.
    Choice 2 The ArrayList expands automatically to fit the addition.
    Choice 3 This type of error cannot be trapped by the code.
    Choice 4 The call throws an ArrayIndexOutOfBounds exception.
    Choice 5 The virtual machine terminates the application.

    Which one of the following is an advantage of providing accessor methods in a class, rather than making the class's fields publicly accessible?
    Choice 1 Accessor methods can ensure data integrity by validating the proposed value. Any value could be written to a public field.
    Choice 2 Accessor methods can prevent memory overwrites by performing bounds checks on arrays. Public fields are not checked for bounds overrun.
    Choice 3 The accessor methods are automatically available to other tasks using the RMI subsystem. Public fields cannot be remotely accessed.
    Choice 4 Accessor methods improve performance by avoiding a context switch in the bytecode interpreter. Accessing a public field requires loading the object's data pointer.

    Choice 5 Accessor methods can be made accessible to classes in other packages. Public fields are only usable from the same package.


    In order for there to be an efficient multi-threaded solution to a problem, what is a key characteristic that the problem must have?
    Choice 1 It interacts with devices and networks.
    Choice 2 The program is to be run on a machine with multiple CPUs.
    Choice 3 There are multiple tasks to be performed that involve very little cooperation.
    Choice 4 A problem takes a long time to solve.
    Choice 5 The program is to be used by multiple people.

    What does polymorphism mean?
    Choice 1 A class can have different behavior depending on its runtime type.
    Choice 2 Dynamic behind the scene code conversion (for example, byte codes to native machine code)
    Choice 3 Dynamic selection of underlying proxies
    Choice 4 A clever form of inheritance
    Choice 5 It is another name for the Object-Oriented paradigm.

    a. Promotes a common understanding of a solution
    b. Guarantees faster code
    c. Avoids redesigning a solution to a problem that has already been solved
    d. Helps to improve the structured design of a solution
    e. Helps to make the code easier and quicker to maintain
    Which of the statements above are reasons why design patterns are very important in software design today?
    Choice 1 a and e
    Choice 2 a, d and e
    Choice 3 c, d, and e
    Choice 4 a, c, d, and e
    Choice 5 a, b, c, d, and e

    public class A {
    int i = 5;
    public static void main(String args[]) {
    A h = new A();
    h.doIt();
    }
    void doIt() {
    i += 1;
    }
    }
    Referring to the sample code above, what is special about the method main() when the Java Virtual Machine (JVM) is started at a command line prompt with the following command: "java A"?
    Choice 1 It is the core method of the Applet.
    Choice 2 It is invoked by the JVM.
    Choice 3 It is an arbitrary method.
    Choice 4 It is doIt()'s parent method.
    Choice 5 It is a method other objects must invoke before invoking other methods.
    int count = 0, i = 0;
    do {
    count += i;
    i++;
    if (count > 5) break;
    } while (i <= 4);
    What is the value of "count" after execution of the code above?
    Choice 1 0
    Choice 2 1
    Choice 3 4
    Choice 4 6
    Choice 5 10

    Which one of the following is true concerning classes (NOT inner or anonymous) that are declared as being protected?
    Choice 1 They can only be accessed by other classes that use the import keyword or the package keyword.
    Choice 2 They can only be members of the default package.
    Choice 3 They cannot be instantiated by any other class.
    Choice 4 They can only be accessed by classes contained within the same package or by subclasses.
    Choice 5 They cannot be accessed by any other class.
    public class Test {
    public static void main(String[] args) {
    int j = 10;
    j <<= 2;
    j = (j - 1) >>> 3;

    System.out.println("j = " + j);
    }
    }
    What is printed on the screen when the sample code above is executed?
    Choice 1 j = 4
    Choice 2 j = 16
    Choice 3 j = 20
    Choice 4 j = 40
    Choice 5 j = 3100
    a. public void foo() {..}
    b. void foo() {..}
    c. protected void foo() {..}
    d. private void foo() {..}
    e. public final void foo() {..}
    Which of the methods above are overridden via inheritance?
    Choice 1 a and c
    Choice 2 a and e
    Choice 3 a, b and c
    Choice 4 a, b and d
    Choice 5 c, d and e
    for (int i = 0; i <= 22; ) {
    if (i <= 10) {
    int j= 2 + i;
    i++;
    // /* let us know */ //
    System.out.println("i: " + i + " j: " + j);
    }
    }
    What is the error in the above code?
    Choice 1 You cannot declare variables inside a for-loop.
    Choice 2 The loop will never terminate.
    Choice 3 Variable "j" is referenced outside its scope.
    Choice 4 You cannot print integer values without converting them to Strings.
    Choice 5 The comment line is not formatted correctly.

    List
    a. There are no mutator, or setter methods.
    b. The class is declared final to prevent subclassing.
    c. The instance fields have private accessibility.
    Which of the features listed above are found in an immutable class, such as the String class?
    Choice 1 a
    Choice 2 a and b
    Choice 3 a and c
    Choice 4 b and c
    Choice 5 a, b, and c

    Why is the JVM considered "secure?"
    Choice 1 It verifies byte code on load.
    Choice 2 No dangerous hand written machine code may be called from the Java program.
    Choice 3 The security manager is built into the JVM and may not be changed.
    Choice 4 It runs on the local machine.
    Choice 5 It only loads classes that are found on the classpath.



    Which one of the following is a checked exception?
    Choice 1 Error
    Choice 2 NullPointerException
    Choice 3 ArithmeticException
    Choice 4 RuntimeException
    Choice 5 Exception


    Which statement about JavaBeans is NOT true?
    Choice 1 A JavaBean is a re-usable software component.
    Choice 2 A JavaBean is immutable; it cannot contain variable data.
    Choice 3 A JavaBean can contain variable data.
    Choice 4 A JavaBean must be serializable.
    Choice 5 A JavaBean generally has one setter for every getter.


    Which statement must be true of a class that implements the Iterator interface?
    Choice 1 It has implementations of the toString() and getNextString() methods.
    Choice 2 It can be used to store associative arrays.
    Choice 3 It is a subclass of List.
    Choice 4 It can work with the StringTokenizer class.
    Choice 5 It has implementations of the hasNext() and next() methods.
    Which one of the following is an advantage of providing accessor methods in a class, rather than making the class's fields publicly accessible?
    Choice 1 Accessor methods can ensure data integrity by validating the proposed value. Any value could be written to a public field.
    Choice 2 Accessor methods can prevent memory overwrites by performing bounds checks on arrays. Public fields are not checked for bounds overrun.
    Choice 3 Accessor methods can be made accessible to classes in other packages. Public fields are only usable from the same package.
    Choice 4 Accessor methods improve performance by avoiding a context switch in the bytecode interpreter. Accessing a public field requires loading the object's data pointer.
    Choice 5 The accessor methods are automatically available to other tasks using the RMI subsystem. Public fields cannot be remotely accessed

    Sample Code
    public synchronized int doIt(int z) {
    int x = 5;
    while (otherMethod()) {
    try { wait(); }
    catch (InterruptedException e) {}
    }
    for (int i = 0; i < z; i++) count += x;
    int result = count - x;
    notify();
    return result;
    }
    Referring to the sample code above, what is the purpose of invoking notify() in method doIt()?
    Choice 1 It changes all the methods of the object containing the doIt() method to be synchronized until the release() method is invoked.
    Choice 2 The notify() method has to be implemented by the developer, so therefore the purpose of invoking notify() is undefined.
    Choice 3 There is no purpose - unless notify() of the java.lang.Object class is overridden it will throw an OperationNotSupportedException.
    Choice 4 It tells waiting threads that they may try to acquire the object's monitor once the running thread has left doIt().
    Choice 5 It informs other threads within the same application that this thread has completed doIt().

    Which one of the following classes is used to read binary data from a file?
    Choice 1 FileOutputStream
    Choice 2 FileReader
    Choice 3 StringReader
    Choice 4 FileInputStream
    Choice 5 FileWriter
    Sample Code
    int count = 0, i = 0;
    do {
    count += i;
    i++;
    if (count > 5) break;
    } while (i <= 4);
    What is the value of "count" after execution of the code above?
    Choice 1 0
    Choice 2 1
    Choice 3 4
    Choice 4 6
    Choice 5 10

    What support does java.io have for preventing malicious code from altering Serialized Objects?
    Choice 1 Checksum
    Choice 2 SSL
    Choice 3 Externalizable
    Choice 4 SerializablePermission
    Choice 5 PGP

    Sample Code
    /**
    * Return a new array with every Object in the array
    * moved to a new random position in the array.
    */
    public Object[] mixup(Object[] array) {
    int numElements = array.length;

    Object[] result = (Object[]) array.clone();
    Arrays.fill(result, null);
    Random random = new Random();

    for (int i=0; i<numElements; i++) {
    int randomIndex;

    do {
    randomIndex = random.nextInt(numElements);
    } while (result[randomIndex] != null);

    result[randomIndex] = array[i];
    }

    return result;
    }
    Referring to the sample code above, which one of the statements is true?
    Choice 1 This code fails with a RuntimeException.
    Choice 2 This code is like Collections.shuffle(list).
    Choice 3 This code will never complete successfully.
    Choice 4 This code is not thread safe.
    Choice 5 This code does not compile.

    What java.util class method is used to sort an array of beans, where the method can specify the order of sorting?
    Choice 1 Collections.sort(beans);
    Choice 2 Algorithms.quickSort(beans);
    Choice 3 ArrayList.sort(beans, ASCENDING);
    Choice 4 Arrays.sort(beans, comparator);
    Choice 5 ArrayList.sort(beans);

    List
    a. final
    b. abstract
    c. virtual
    d. static
    Which of the keywords above are often used with polymorphic methods?
    Choice 1 a
    Choice 2 b
    Choice 3 c
    Choice 4 a, b, and d
    Choice 5 a, b, c, and d

    Text
    Invoking a method can represent a significant amount of overhead in a program; as such, some compilers will perform an optimization called "method inlining." This optimization will remove a method call by copying the code inside the method into the calling method.
    Referring to the text above, which one of these statements is true?
    Choice 1 The developer of inlined methods must copy and paste the code that is to be inlined into another method.
    Choice 2 The performance benefits should be balanced against the increased chance of a RuntimeException.
    Choice 3 It allows the use of getter and setter methods to execute nearly as fast as direct access to member variables.
    Choice 4 It prevents code from executing in a way that follows object-oriented encapsulation.
    Choice 5 This optimization will only occur if the relevant methods are declared volatile.

    Sample Code
    // Assume DateValue is a class that represents a date
    // in some unspecified format.
    public void printIfEqual(DateValue dv1, DateValue dv2) {
    if (dv1 == dv2) {
    System.out.println("Dates " + dv1 + " and " + dv2
    + "are the same day");
    }
    }
    What is the potential usage error in the above code?
    Choice 1 System.out.println() may throw an IOException that is not caught or rethrown by printIfEqual.
    Choice 2 The println statement may fail if DateValue does not implement toString().
    Choice 3 The dv1 == dv2 comparison may fail if DateValue does not override the equals() method.
    Choice 4 The two objects could represent the same date but not satisfy dv1 == dv2.
    Choice 5 System.out may have been redirected away from the console.

    Sample Code
    The following code defines the Example class.

    package com.brainbench.javaexamples;

    public class Example {
    public static final int VALUE = 10;

    // Additional code not shown.

    }
    Referring to the sample code above, what classes modify the value of Example.VALUE?
    Choice 1 Inner classes of Example only
    Choice 2 Classes in the same package only
    Choice 3 Classes in the same source code unit only
    Choice 4 Classes in Java standard packages only
    Choice 5 None of the classes

    Scenario
    a. Scrollable is an interface.
    b. Writable is an interface.
    c. TextScreen is an abstract class.
    d. VideoScreen is a class that extends TextScreen and implements both Scrollable and Writable.
    Referring to the scenario above, which one of the following statements is always true?
    Choice 1 A Scrollable can be cast to a VideoScreen.
    Choice 2 A VideoScreen cannot be passed to a method that expects a TextScreen.
    Choice 3 A VideoScreen cannot be passed to a method that expects a Scrollable.
    Choice 4 A VideoScreen cannot be passed to a method that expects a Writable.
    Choice 5 A VideoScreen can be cast to a Scrollable.

    List
    a. Class loading
    b. Class verification
    c. Garbage collection
    d. Code execution
    e. Source code generation
    f. Security enforcement
    Which of the tasks above are performed by the JVM?
    Choice 1 a, b, and e
    Choice 2 a, b, c, and f
    Choice 3 a, b, e, and f
    Choice 4 a, c, e, and f
    Choice 5 a, b, c, d, and f

    Sample Code
    public class Test {
    public static void main(String[] args) {
    foo("foo");
    foo(new String("foo"));
    foo("bar");
    }

    private static void foo(String arg) {
    if (arg == "foo") {
    arg = "1";
    } else if (arg.equals("foo")) {
    arg = "2";
    } else if (arg.equals("baR")) {
    arg = "3";
    } else {
    arg = "4";
    }

    System.out.print(arg);
    }
    }
    What does the code above print?
    Choice 1 113
    Choice 2 114
    Choice 3 124
    Choice 4 223
    Choice 5 224

    Sample Code
    public class Test {
    public static void main(String[] args) {
    int j = 10;
    j <<= 2;
    j = (j - 1) >>> 3;

    System.out.println("j = " + j);
    }
    }
    What is printed on the screen when the sample code above is executed?
    Choice 1 j = 4
    Choice 2 j = 16
    Choice 3 j = 20
    Choice 4 j = 40
    Choice 5 j = 3100


    Which one of the following is true concerning classes (NOT inner or anonymous) that are declared as being protected?
    Choice 1 They can only be accessed by other classes that use the import keyword or the package keyword.
    Choice 2 They can only be accessed by classes contained within the same package or by subclasses.
    Choice 3 They cannot be accessed by any other class.
    Choice 4 They can only be members of the default package.
    Choice 5 They cannot be instantiated by any other class.

    For which one of the following problems does java.util NOT offer tools?
    Choice 1 Generating random numbers
    Choice 2 Time handling
    Choice 3 Object destruction
    Choice 4 Array sorting
    Choice 5 Internationalization

    List
    a. There are no mutator, or setter methods.
    b. The class is declared final to prevent subclassing.
    c. The instance fields have private accessibility.
    Which of the features listed above are found in an immutable class, such as the String class?
    Choice 1 a
    Choice 2 a and b
    Choice 3 a and c
    Choice 4 b and c
    Choice 5 a, b, and

    Sample Code
    public class TestEx {
    static class Ex1 extends Exception {}
    static class Ex2 extends Ex1 {}
    static class Ex3 extends Exception {}

    static void method1() throws Ex1, Ex2, Ex3 {
    throw new Ex2(); }

    public static void main(String args[]) {
    try {
    method1();
    method1();
    }
    catch (Ex3 e) { System.out.print("C"); }
    catch (Ex2 e) { System.out.print("B"); }
    catch (Ex1 e) { System.out.print("A"); }
    catch (Exception e) { System.out.print("D"); }
    finally { System.out.println("F"); }
    }
    }
    What is the output of the code fragment shown above?
    Choice 1 BA
    Choice 2 BADF
    Choice 3 BBF
    Choice 4 BD
    Choice 5 BF

    Which one of the following classes does NOT appear in java.lang?
    Choice 1 Runtime
    Choice 2 String
    Choice 3 Object
    Choice 4 Math
    Choice 5 HashMap

    List
    a. Promotes a common understanding of a solution
    b. Guarantees faster code
    c. Avoids redesigning a solution to a problem that has already been solved
    d. Helps to improve the structured design of a solution
    e. Helps to make the code easier and quicker to maintain

    Which of the statements above are reasons why design patterns are very important in software design today?
    Choice 1 a and e
    Choice 2 a, d and e
    Choice 3 c, d, and e
    Choice 4 a, c, d, and e
    Choice 5 a, b, c, d, and e

    Which one of the following operations is NOT supported by java.lang.Runtime?
    Choice 1 Dynamically unload Class files.
    Choice 2 Cause the Java Virtual Machine (JVM) to exit.
    Choice 3 Request the garbage collector to be active.
    Choice 4 Execute native operating system commands.
    Choice 5 Add shutdown hooks.

    Which one of the following keywords has the highest associated active CPU usage?
    Choice 1 Continue
    Choice 2 Throw
    Choice 3 While
    Choice 4 Break
    Choice 5 Final

    What is a difference between the java.io.FileInputStream and java.io.RandomAccessFile classes for reading files?
    Choice 1 RandomAccessFile lets you control the position for the next read.
    Choice 2 RandomAccessFile is an abstract class.
    Choice 3 RandomAccessFile uses a buffered output stream and is usually faster than FileInputStream.
    Choice 4 RandomAccessFile can only be used for char and byte datatypes.
    Choice 5 RandomAccessFile does not contain methods for reading data.


    List
    a. Fully qualified class name
    b. Method name
    c. Method argument names
    d. Method argument types
    e. Throw exceptions
    Which of the components shown above are used to uniquely identify a method?
    Choice 1 a, b, and d
    Choice 2 b, c, and d
    Choice 3 b, d, and e
    Choice 4 a, b, c, and d
    Choice 5 b, c, d, and e

    How do you store a copy of an object at the same time other threads are changing the object's properties?
    Choice 1 You do not need to do anything special since all ObjectOutputStream methods are synchronized.
    Choice 2 Clone the object in a block synchronized on the object and serialize the clone.
    Choice 3 Implement java.io.Externalizable instead of java.io.Serializable.
    Choice 4 Give the thread performing storage a higher priority than other threads.
    Choice 5 Override writeObject() in ObjectOutputStream to make it synchronized.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java Fundamental questions

    Welcome jmsrajeev

    That seems a bit much to expect people to read. I suggest you work on one question at a time.
    Post the relevant code with the original question, and your question about it/them

  3. The Following User Says Thank You to jps For This Useful Post:

    sada (July 28th, 2014)

Similar Threads

  1. List of my Java3D Questions, and Proguard questions
    By Zachary1234 in forum Java SE APIs
    Replies: 0
    Last Post: November 16th, 2012, 09:40 PM
  2. Fundamental knowledge before starting Java programming.
    By wsung in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2012, 05:42 AM
  3. New to Java some questions
    By unorthadox in forum Java Theory & Questions
    Replies: 3
    Last Post: August 6th, 2012, 09:22 AM
  4. Help in java questions
    By Normal9ja in forum Java Theory & Questions
    Replies: 3
    Last Post: September 21st, 2011, 08:42 PM
  5. Java Questions! :)
    By xs4rdx in forum Java Theory & Questions
    Replies: 0
    Last Post: February 21st, 2010, 08:40 AM