Java Programming Concepts, Exams of Java Programming

A wide range of java programming concepts, including object-oriented programming, data types, control structures, arrays, and more. It presents various questions and answers related to these topics, which can be useful for students preparing for exams or assignments in java programming courses. Insights into fundamental java programming principles and can serve as a valuable resource for both university students and lifelong learners interested in expanding their knowledge of java development.

Typology: Exams

2023/2024

Available from 08/03/2024

Classrep02
Classrep02 🇺🇸

3

(2)

2.9K documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JAVA (EXAM 3) Questions & Answers
100%Correct!!
All classes implement an abstract data type. - ANSWERFalse
In a single statement, define and initialize a reference variable for an ArrayList named frameScores
that stores items of type Integer. - ANSWERArrayList frameScores = new ArrayList();
Given: ArrayList itemsList = new ArrayList(); What is the initial size of the ArrayList? - ANSWER0; Zero
The _________ operator is used to explicitly allocate an object. - ANSWERnew
Given: Employee clerk1 = new Employee() ;. clerk1 refer to an object. - ANSWERTrue
Define a private field named salary of type int for Employee class: - ANSWERprivate int salary;
Which of the following commands is used to run a java program? - ANSWERjava
The constructor has the same name as the class. The constructor method has no return type, not
even void. - ANSWERTrue
Class Dwelling has data members door1, door2, door3. A class House is derived from Dwelling and
has data members wVal, xVal, yVal, zVal. The definition and initialization House h = new House();
creates how many data members? - ANSWER7; seven
Using a JFormattedTextField variable named numItersField, write a statement that creates a
JFormattedTextField that uses a NumberFormat to display integers. - ANSWERnumItersField = new
JFormattedTextField(NumberFormat.getIntegerInstance());
The block tag specification below creates a reference to ElapsedTime's printTime() method. @see
ElapsedTime#printTime() - ANSWERTrue
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Java Programming Concepts and more Exams Java Programming in PDF only on Docsity!

JAVA (EXAM 3) Questions & Answers

100%Correct!!

All classes implement an abstract data type. - ANSWERFalse In a single statement, define and initialize a reference variable for an ArrayList named frameScores that stores items of type Integer. - ANSWERArrayList frameScores = new ArrayList(); Given: ArrayList itemsList = new ArrayList(); What is the initial size of the ArrayList? - ANSWER0; Zero The _________ operator is used to explicitly allocate an object. - ANSWERnew Given: Employee clerk1 = new Employee() ;. clerk1 refer to an object. - ANSWERTrue Define a private field named salary of type int for Employee class: - ANSWERprivate int salary; Which of the following commands is used to run a java program? - ANSWERjava The constructor has the same name as the class. The constructor method has no return type, not even void. - ANSWERTrue Class Dwelling has data members door1, door2, door3. A class House is derived from Dwelling and has data members wVal, xVal, yVal, zVal. The definition and initialization House h = new House(); creates how many data members? - ANSWER7; seven Using a JFormattedTextField variable named numItersField, write a statement that creates a JFormattedTextField that uses a NumberFormat to display integers. - ANSWERnumItersField = new JFormattedTextField(NumberFormat.getIntegerInstance()); The block tag specification below creates a reference to ElapsedTime's printTime() method. @see ElapsedTime#printTime() - ANSWERTrue

Given a JFrame variable named frame and a JTextField variable named nameField, write a statement that adds nameField to the frame. - ANSWERframe.add(nameField); Assume an empty JFrame object named appFrame. The program called the setVisible() method correctly (i.e., appFrame.setVisible(true);), but the frame is not visible on the screen. The frame should be 500 pixels wide and 300 pixels tall. - ANSWERappFrame.setSize(500, 300); Which of the following is NOT a characteristics of Object Oriented Programming? - ANSWERrevolution Using the GridBagConstraints variable named layoutConstraints, write a statement that would add 5 pixels of padding to the left and right of a GUI component. The top and bottom edges of the component should not have padding. - ANSWERlayoutConstraints.insets = new Insets(0, 5, 0, 5); Given the JTextField variable named weeklyHoursField, write a statement that makes weeklyHoursField editable by users. - ANSWERweeklyHoursField.setEditable(true); A private helper method may not call another private helper method. - ANSWERFalse Which access modifier allows a class be used by every class in the program regardless of the package in which either is defined? - ANSWERpublic Suppose your program uses an object called Student. Which of the following shall NOT be part of the Student object? - ANSWERgetPrice() By default, the equals() method tests the equality of the contents of two Objects , not the equality of their references. - ANSWERFalse Suppose that public Student(String name, String id){// code} is the only constructor the Student class has. Which of the following is correct? - ANSWERStudent s = new Student(â Tomâ , â 1234â ); A derived class may define a member method having the same name as the base class. Such a member method _______________ the method of the base class. - ANSWERoverrides Suppose that we have the following code. Student s1 = new Student(); Student s2; Which of the following is true after the above two line of code are executed? - ANSWERs1 refers to valid object

What is the output of the displayed code? Public class Test { public static void main( String args[]) { int intArray[] = new int [5]; .... for (int outerIndex = 1; outerIndex < intArray.length; ++outputIndex) { for (int innerIndex = 1; innerIndex < dbIArray.length; ++innerIndex) { dblArray [ innerIndex ] = innerIndx * outerIndex; } } for (int theIndex = 1; theIndex < dbLArray.length; ++theIndex) System.out.print( dbLArray[ theIndex } + ", "); } } - ANSWER4.0, 8.0, 12.0, 16.0, 20.0, 24.0, A key advantage of javadoc comments is that the documentation is close to the source code so is easier to keep consistent with the source code. - ANSWERTrue The while loop syntax is: while(condition); {//loop body statements} - ANSWERFalse Is the following code defines an enumeration type? public enum LightState {RED; GREEN; YELLOW; DONE} - ANSWERFalse Is this method correct for squaring an integer? public static int sqr(int a) { int t; t = a * a; return a;} - ANSWERFalse Which of the following is correct for loop structure? - ANSWERfor(int x = 1; x < 10; x++){}

If we rewrite for(int i = 1; i < 10; i++){//loop body} as a while loop. Which of the following is in loop body? - ANSWERi++; Each method call creates a new set of local variables, forming part of what is known as a _____________ - ANSWERstack frame Which of the following increase value of x by 1? Assume x is int type variable. - ANSWERx += 1; x = x + 1; x++; ++x; Each execution of the loop body is called an ___________ - ANSWERx != 0 !(x == 0) How many times the following while loop will be executed? Assume int x = 5; while(x > 0){ x--; } - ANSWER A local variable's scope extends from a method's opening brace to the method's closing brace. - ANSWERFalse Which correctly passes two integer arguments for the method call calcVal(...)? - ANSWER(99, 4+5) Suppose method void printAge(_________) has an int parameter userAge. Fill in the missing parameter. - ANSWERint userAge If a method's local variable has the same name as a field, the name will refer to the local variable. - ANSWERTrue

How many times the statement x++; executed? for(int i = 0; i < 9; i++);{ for(int j = 0; j < 9; j++); { x++;}}

  • ANSWER The programmer must use special notation to pass an array by reference; otherwise an array is passed by value. - ANSWERFalse Suppose that int[] arr = new int[4]; has been initialized as 1, 2, 3, 4. What is the output of the following code? for(int i = 0; i < arr.length; i++) System.out.print(arr[i]); - ANSWER A return type of void indicates that a method does not return any value, in which case the return statement should simply be: ____________ - ANSWERreturn; When the program is run, what CD cost information will be displayed? - ANSWER8. For a method with three integer inputs, about 3-5 test vectors is likely sufficient for testing purposes.
  • ANSWERFalse Define and initialize a two dimensional array of integers named dataVals with 4 rows and 7 columns using default element values. - ANSWERint[][] dataVals=new int[4][7]; To Avoid writing redundant code is one of the reasons that we write method - ANSWERfalse A FIXME comment is commonly used to indicate program parts to be fixed or added. - ANSWERTrue Write a statement that converts the text representation of an integer within String numberText, storing the value in an int variable numLanes. - ANSWERnumLanes = Integer.parseInt(numberText);

Which access modifier allows a class only be used by self? - ANSWERprivate A private helper method may not call another private helper method. - ANSWERFalse Given the JButton variable named convertButton, write a statement that adds an ActionListener to convertButton. Use the "this" keyword. - ANSWERconvertButton.addActionListener(this); Given the JLabel variable named nameField, write a statement that creates a JLabel with the text "Name:". - ANSWERnameField = new JLabel("Name:"); Which of the following is NOT a characteristics of Object Oriented Programming? - ANSWERrevolution Assume an empty JFrame object named appFrame. The program called the setVisible() method correctly (i.e., appFrame.setVisible(true);), but the frame is not visible on the screen. The frame should be 500 pixels wide and 300 pixels tall. - ANSWERappFrame.setSize(500,300); Write a statement that sets the text of a JTextField component named nameField to "Mary". - ANSWERnameField.setText("Mary"); The block tag specification below creates a reference to ElapsedTime's printTime() method. @see ElapsedTime#printTime() - ANSWERTrue Write a single statement that gets the value of a JFormattedTextField called speedField as a double and stores it in a variable of type double called carSpeed. - ANSWERcarSpeed = ((Number)speedField.getValue()).doubleValue(); Programmers commonly draw class inheritance relationships using UML notation. What UML stands for? - ANSWERUnified Modeling Language A programmer must provide at least a default constructor. Otherwise, the class will have no constructors. - ANSWERFalse

Which of the following is not javadoc tags? - ANSWER@argument The loop body of a do-while loop will be executed at least once - ANSWERTrue enum type definition occurs inside the method. - ANSWERFalse Returning the incorrect variable from a method is a common error. - ANSWERTrue Which of the following is correct for loop structure? - ANSWERfor(int x = 1; x < 10; x++){} every while loop can be rewrite as a for loop - ANSWERFalse A return address indicates the value returned by the method. - ANSWERFalse Which of the following decrease value of x by 1? Assume x is int type variable. - ANSWERx -= 1; x = x - 1; x--; In a while loop, once execution enters the loop body, execution continues to the body's end even if the loop condition expression becomes false midway through. - ANSWERTrue Write loop condition: x and y are not both zero. - ANSWERx != 0 || y != 0 !(x == 0 && y == 0) How many times the following while loop will be executed? Assume int x = 5; while(x >= 0){ x--; } - ANSWER

A return type of void indicates that a method does not return any value, in which case the return statement should simply be: ______________. - ANSWERreturn; Given a method definition: void calcVal(int a, int b, int c) and given int variables i, j, and k, which are valid arguments in the call calcVal(...)? - ANSWER(i+5, j+6, k+10) void myMthd(int userNum + 5) is a valid method head. - ANSWERFalse A local variable's scope extends from a method's opening brace to the method's closing brace. - ANSWERFalse In Java, what is the most efficient way to store and randomly access a sequence of objects? - ANSWERarray How many times the statement x++; executed? for(int i = 0; i < 9; i++);{ for(int j = 0; j < 9; j++) { x++;}}

  • ANSWER In contrast to primitive types, a method parameter of array (or object) type is pass by ____________.
  • ANSWERreference Suppose that int[] arr = new int[4]; has been initialized as 1, 2, 3, 4. What is the output of the following code? for(int i = 1; i < arr.length; i++) System.out.print(arr[i]); - ANSWER Given: void printSomething (int num1) { ... }. Then return 0; a valid return statement. - ANSWERFalse What is the best routine to interchange the values stored in location1 and location2? - ANSWERtemp = location2; location2 = location1; location1 = temp; For a method with three integer inputs, about 3-5 test vectors is likely sufficient for testing purposes.
  • ANSWERFalse

The block tag specification below creates a reference to ElapsedTime's printTime() method. @see ElapsedTime#printTime() - ANSWERTrue Given a JFrame variable named frame and a JTextField variable named nameField, write a statement that adds nameField to the frame. - ANSWERframe.add(nameField); Assume an empty JFrame object named appFrame. The program called the setVisible() method correctly (i.e., appFrame.setVisible(true);), but the frame is not visible on the screen. The frame should be 500 pixels wide and 300 pixels tall. - ANSWERappFrame.setSize(500, 300); The JVM automatically performs runtime polymorphism to determine the correct method to call. - ANSWERTrue Using the GridBagConstraints variable named layoutConstraints, write a statement that would add 5 pixels of padding to the left and right of a GUI component. The top and bottom edges of the component should not have padding. - ANSWERlayoutConstraints.insets = new Insets(0, 5, 0, 5); Given the JTextField variable named weeklyHoursField, write a statement that adds an ActionListener to weeklyHoursField. Use the "this" keyword. - ANSWERweeklyHoursField.addActionListener(this); A public member method may not call another public member method. - ANSWERFalse Which access modifier allows a class be used only in other classes within the same package, i.e. known as package private? - ANSWERno specifier Suppose your program uses an object called Student. Which of the following shall NOT be part of the Student object? - ANSWERgetPrice() All classes can access Object's public and protected methods (e.g., toString() and equals()) even if such methods are not explicitly overridden. - ANSWERTrue Suppose that public ShoppingItem(double price, int quantity) is the only constructor the ShoppingItem class has. Which of the following is correct? - ANSWERShoppingItem item = new hoppingItem(3.85, 5);

Suppose that we have the following code. Student s1 = new Student(); Student s2 = s1; Which of the following is true after the above two line of code are executed? - ANSWERs1 and s2 both refer to valid objects Assigning a different value to a reference parameter within the method deletes the original object. - ANSWERFalse A ____________ method may modify the class fields. - ANSWERmutator If every line of code was executed at least once (complete code coverage) and all tests passed, the class must be bug free. - ANSWERFalse Within a member method, the implicitly-passed object reference is accessible via the keyword ____________. - ANSWERthis Suppose that Team class has an object of Person class as attribute. True or False: Person class uses Team class. - ANSWERFalse Define a variable called gameScore that refers to a primitive wrapper object with an int value of 81. Use the object initialization style. - ANSWERInteger gameScore = new Integer(81); Write a statement that assigns the int representation of the value held by the Integer objects totalPins to a primitive int variable pinScore. - ANSWERpinScore = totalPins.intValue(); Each item in array is directly accessible. - ANSWERTrue Given array firstList with size 4 and element values, 33, 44, 55, 66, and array secondList with size 4 and elements values 0, 0, 0, 0. firstList = secondList; copies 0s into each firstList element. - ANSWERFalse What is contained in each element of the array when the following statements are executed? int numbers[]; numbers = new int[2]; - ANSWER

How many times the following while loop will be executed? Assume int x = 5; while(x != 0){ x -= 2; } - ANSWERinfinite times A ______________ is a list of statements surrounded by braces. - ANSWERblock Given a method definition: void calcVal(int a, int b, int c) what value is assigned to b during this method call: calcVal(42, 55, 77); - ANSWER Call a method void printAge(int x), passing the value 21 as an argument. - ANSWERprintAge(21); If a method's local variable has the same name as a method parameter, the name will refer to the local variable - ANSWERFalse Which of the following correctly shows the syntax for accessing the value in an array element? - ANSWERarray-identifier[int-index-value]; How many times the statement x++; executed? for(int i = 0; i < 9; i++);{ for(int j = 0; j < 9; j++); { x++;}}

  • ANSWER The programmer must use special notation to pass an array by reference; otherwise an array is passed by value. - ANSWERFalse Suppose that int[] arr = new int[4]; has been initialized as 1, 2, 3, 4. What is the output of the following code? for(int i = 0; i < arr.length; i++) System.out.print(arr[i]); - ANSWER A return type of _________ indicates that a method does not return any value, in which case the return statement should simply be: return; - ANSWERvoid Which song is printed to the screen last? - ANSWERShake It Up and Go A good programmer takes the time to test all possible input values for a method. - ANSWERFalse How many total integers elements are in an array with 4 rows and 7 columns? - ANSWER

what are the reasons for writing methods? - ANSWERImprove program readability Modular program development Avoid writing redundant code A good programming process is to write the entire program, then incrementally remove bugs one at a time. - ANSWERFalse Assume that int[] a = now int [9]; Write one line of code to assign the first element in array ro be 100.

  • ANSWERa[0] = 100; Given array firstList with size 4 and element values 33, 44, 55, 66 and array secondList with size 4 and elements values 0,0,0,0, firstList = secondList; copies 0s into each firstList element. - ANSWERFalse What is the index of the last element in an array? - ANSWERThe size of the array minus one A break statement in a loop causes an immediate exit of the loop - ANSWERTrue What is the output of the program? - ANSWERAn IndexOutOfBoundsException occurs during runtime A key advantage of javadoc comments is that a change to a comment in the java source automatically updates documentation on an HTML webpage - ANSWERTrue The do-while loop syntax is: do{//loop body statements } while(condition) - ANSWERFalse In many situation, an eumeration is clearer, requires less codes, and is less prone to error - ANSWERTrue Is this method correct for squaring an integer? public static int sqr(int a) {int t; t = a* a;} - ANSWERFalse Which of the following is correct for loop structure - ANSWERfor(int x = 1; x <10; x++){}

How many times the inner loop body executed? for(int i = 0; i < 9; j++) { x++;}} - ANSWER The programmer must use special notation to pass an array by reference; otherwise an array is passed by value. - ANSWERFalse Suppose that int[] arr = new int[4]; has been initialized as 1,2,3,4. What is the output of the following code? for(int i = 0, i < arr.length, i++) System.out.print(arr[i]); - ANSWER A return type of void indicates that a method does not return any value, in which case the return statement should simply be ________________. - ANSWERreturn; For method, border cases might include 0, a very large negative number, and a very large positive number. - ANSWERTrue How many elements are in the array defined as: char[][] streetName = new char[20][50]; - ANSWER To Avoid writing redundant code is one of the reasons that we write method - ANSWERFalse Experienced programmers develop programs incrementally, meaning they create a simple program version, and then growing the program little-by-little into successively more-complete versions. - ANSWERTrue An ADT's interface is defined by the ADT's public member method declarations. - ANSWERTrue Assign the Integer element at index 8 of integer ArrayList frameScores to an Integer variable currFrame. - ANSWERcurrFrame = frameScores get(8); Given ArrayList itemsList = new ArrayList(); After itemsList set(0,99);, what is the ArrayList's size? - ANSWERthe code has error The ______________ construct defines a new type that can group data and methods to form an obejct - ANSWERclass

The programmer defines each field after the ________ access modifier, making clear that a class user cannot directly access the fields. - ANSWERprivate Define a private field named salary of type int for Employee class, - ANSWERprivate int salary; Write a command using the Java compiler to compile a program consisting of classes Ingredient, Recipe, and FamilyCookbook. Preserve this class ordering. - ANSWERjavac Ingredient.java Recipe.java FamilyCookbook.java The constructor has the same name as the class. The constructor method has return type void. - ANSWERFalse Class Dwelling has data members door1, door2. door3. A class House is derived from Dwelling and has data members wVal, xVal, yVal, zVal. The definition and initializa - ANSWER Write a single statement that gets the value of a JFormattedTextField called speedField as a double and stores it in a variable of type double called carSpeed. - ANSWERcarSpped = ((Number)speedField.getValue()).doubleValue(); The @author block tag can be used to specify a method's author. - ANSWERFalse Write a statement that sets the text of a JTextField component named nameField to "Mary". - ANSWERnameField.setText("Mary"); Assume an empty JFrame object named appFrame. - ANSWERappFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); The relationship between House and Window is: Has-a-relationship. - ANSWERTrue Given the JLabel variable named nameField, write a statement that creates a JLabel with the text "Name". - ANSWERnameField = new JLabel("Name: "); Given the JTextField variable named weeklyHoursField, write statement that makes weeklyHoursField editable by users. - ANSWERweeklyHoursField.setEditable(true);