1/40
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What is the output of the following code?
class A {
public void do_it() {
System.out.println("A");
}
}
class B extends A {
public void do_stuff() {
System.out.println("B");
}
}
class Main {
public static void main(String[]args)
{
A myClass=new A();
myClass=new B();
myClass.do_it();
}
}
-A
-B
-AB
-This code does not compile
-A
Which of these is correct?
- a base class is a child class or derived class
- a parent class is a subclass of its child
- a child class is a super class of its parent
- a base class is a parent class or super class
-a base class is a parent class or super class
The relationship between a parent class and a child class is referred to as a(n) ________ relationship.
- has-a
- alias
- is-a
- instance-of
- is-a
A structure that contains attributes and methods is?
-A class
-None of the above
-Inheritance
-Encapsulation
-A class
What is the output of the following code?
class Stuff {
public int number=1;
}
class Main {
public static void main(String[] args) {
Stuff myStuff = new Stuff();
myStuff.number+=3;
System.out.println("myStuff.number:"+myStuff.number);
myStuff = new Stuff();
System.out.println("myStuff.number:"+myStuff.number);}}
-myStuff.number:4
-myStuff.number:1
Consider the following code for class XYZ
public class XYZ
{
private int myNum;
public XYZ (int n1) {
myNum = n1;
}
}
Which of the following statements is TRUE about class XYZ
-A subclass cannot inherit from class XYZ
-Class XYZ inherits from the Object class
-Class XYZ has a child class called the Object class
-Class XYZ cannot be instantiated into an object
-Class XYZ inherits from the Object class
Which line or lines of code below will cause compile errors?
class Parent {
public int x;
}
class Child extends Parent {
public void doStuff() {
System.out.println("I'm a child");
}
}
class Main {
public static void main(String[] args) {
Parent p1 = new Parent();
Child c1 = new Child();
Parent p2 = new Child();
p1.doStuff(); //Line 1
c1.doStuff(); //Line 2
p2.doStuff(); //Line 3
((Child)p2).doStuff(); //Line 4
}
}
-Line 1
Line 2
Line 3
Line 4
-Line 1
Line 3
Line 4
-Line 1
Line 3
-Line 1
-Line 1
Line 3
import java.util.ArrayList;
class Main {public static void main(String[] args)
{ArrayList
myNumbers.add(10)
;myNumbers.add(20);
myNumbers.add(30);
myNumbers.add(40);
System.out.println(myNumbers.get(myNumbers.size()-1));}}
-10
-20
-30
-40
-40
What is the output of the following statements:
//Java:
String s="Hi";
for(int i=0;i<3;i++) {
System.out.print("S"+s);
}
-Hi
-HiHiHi
-SHiSHiSHi
-SHiSHi
-SHiSHiSHi
We cannot create instance of
-Abstract class
-Parent class
-Child class
-Nested class
-Abstract class
What does the following code output?
int x=10;
for(int i=0;i<3;i++) {
x+=5;
}
System.out.println(x);
-25
-20
-30
-0
-25
The following code will compile and output 8?
class Stuff {
public static int x=7;
public static void do_stuff()
{
x++;System.out.println(x);
}
}
class Main {
public static void main(String[] args) {
Stuff.do_stuff();
}
}
-True
-False
-True
Constructor can return a value
-True
-False
-False
In a recursive method the base case condition's actions move the algorithm towards the base case and termination.
-True
-False
-False
Exceptions occur during the execution of a method.
-True
-False
-True
True/False, if you enter the letter B into the following code it'll throw the following exceptions:
Java: NumberFormatException,
C#: FormatException
import java.util.Scanner;
class Main {public static void main(String[] args) {
Scanner myScan=new Scanner(System.in); System.out.println("Enter a number");
try
{
String answer=myScan.nextLine(); int num=Integer.parseInt(answer);
}
catch(Exception e) {
System.out.println("Error: "+e);}
}
}
-False
-True
-True
Which of the following statements about recursion are true?
Select all that apply
-Most recursive algorithm can be written iteratively.
-Every recursive algorithms must have at least one base condition.
-In a recursive definition, an object is defined in terms of a simpler case of itself
-Every recursive algorithm takes exactly one parameter
-Most recursive algorithm can be written iteratively.
-Every recursive algorithms must have at least one base condition.
-Every recursive algorithm takes exactly one parameter
Exception handling separates error-handling code from normal programming tasks, thus making programs easier to read and modify.
-True
-False
-True
The following recursive method takes in an array of characters, and finds the highest letter alphabetically in the array. i.e. if passed the following characters: x a b p s, it should return x because x is the last of those alphabetically.
Choose the correct base condition that completes the code correctly from the choices below.
public static char last_letter_used(char[] charArray, int position) {
//Base condition goes here
else {
if(charArray[position]>last_letter_used(charArray,position+1)) {
return charArray[position];
}
else {
return(last_letter_used(charArray,position+1));
}
}
}
if(position==charArray.length-1) {
return charArray[position];
}
Given the following code:
public static int do_stuff(int x) {
if(x==0) {
return 1;
}
else {
return 1 + do_stuff(x-2);
}
}
What is returned from this code if called as follows:
do_stuff(3);
-1
-2
-3
-This code crashes with a stack overflow
-This code crashes with a stack overflow
Refer to the following recursive method.
public int mystery(int n)
{
if (n < 0)
return 2;
else
return mystery(n -1) + mystery(n - 3);
}
What does it return if you call it with mystery(3);
-6
-12
-10
-8
-12
Exception handling enables a method to throw an exception to its caller.
-True
-False
-true
The orders in which exceptions are specified in a catch block is not important.
-True
-False
-False
Refer to method mystery:
public int mystery(int n, int a, int d){
if (n==1)
return a;
else
return d + mystery(n - 1, a, d);
}
What value is returned by the call mystery(3, 2, 6)?
-8
-20
-10
-14
-14
Given the following code:
public static int do_stuff(int x) {
if(x==0) {
return 1;
}
else {
return 1 + do_stuff(x-2);
}
}
What is returned from this code if called as follows:
do_stuff(4);
-1
-2
-3
-This code crashes with a stack overflow
-3
Given the following method:
public static int recursive(int n,int m) {
if(n<=1) {
return 1;
}
else {
return m+recursive(n/2,m*2);
}
}
What is the value of x after the following call:
int x = recursive(6,8);
-25
-17
-19
-23
-25
Given the following method:
public static int pblmOne(int myNum){
if (myNum < 2) {
return 3;
}
else {
return 2 + pblmOne(myNum-2);
}
}
If it is called as follows, what is the output:PRINT(pblmOne(10));
-13
-15
-3
-10
-13
The try-catch construct consists of one try block and one or more catch blocks. The try block contains the code that could throw exceptions.
-True
-False
-True
Given the following method:
public static int do_stuff(int x) {
if(x<=0) {
return 1;
}
else {
return do_stuff(x-1)+do_stuff(x-2);
}
}
What will the value of ans be after the method is called like this:
int ans=do_stuff(2);
-1
-2
-3
-4
-3
If a recursive method has no base case (bottom), or if the base case is never reached, it will become infinite and the result will be stack overflow error.
-True
-False
-True
What is the output of the following code?
class Main {
public static String question(String x) {
if(x.length()==0) {
return x;
}
else {
return "X"+question(x.substring(0,x.length()-1));
}
}
public static void main(String[] args) {
String y=question("ABCDE"); System.out.println(y);
}
}
-ABCDE
-EDCBA
-XEXDXCXBXA
-XXXXX
-XXXXX
Given the following method:
public static int recursive(int n) {
if(n<=1) {
return 1;
}
else {
return 2+recursive(n/2);
}
}
What is the value of x after the following call:
int x = recursive(3);
-0
-1
-2
-3
-3
Given the following method:
public static int myFunc(int x, int y) {
if(x==0) {
return y;
}
else {
return myFunc(x-1,y+1);
}
}
What is the value of x after the following call:int x = myFunc(0,4);
-4
-6
-8
-Infinite recursion
-4
The code within the finally block is always executed, no matter how the program flow leaves the try block. This guarantees that the finally block will be executed even if an exception is thrown.
-True
-False
-True
What is the output of the following code:
class Main {
public static void do_stuff() {
int[] myNums = new int[5]; myNums[5]=1;
}
public static void main(String[] args) {
try {
do_stuff();
}
catch(Exception e) {
System.out.println("A");
}
finally {
System.out.println("X"); }
}
}
-No output
-A
-A
X
-X
-A
X
What is the output of the following code:
class Wrong extends Exception { public Wrong(String message) { super(message);
}
}
class Main {
public static void doStuff(int x) throws Wrong {
if(x<0) {
throw new Wrong("Too Low");
}
else if(x==0) {
System.out.println(x); }
else {
doStuff(x-1);
System.out.println(x);
}
}
public static void main(String[] args) {
try {
doStuff(3);
System.out.println("---"); doStuff(-1);
} catch(Exception e) { System.out.println(e.getMessage());
}
}
}
0
1
2
3
---
Too Low
What is the output of the following code:
class Main {
public static void main(String[] args) {
int[] myArray = new int[5];
try {
for(int i=0;i<=5;i++) {
myArray[i]=i; } System.out.println(myArray[3]);
} catch(Exception e) {
System.out.println("Something went wrong");
}
}
}
-Something went wrong
-Something went wrong
3
-3
-2
-Something went wrong
After executing the following code, what will be the contents of A.txt?
import java.io.*;
import java.io.File;i
mport java.io.IOException;
class Main {
public static void write_file(String filename, String line, int x) {
try { File myFile=new File(filename); PrintWriter theFile = new PrintWriter(myFile);
for(int i=0;i
A.txt will contain the word Hi on three lines
Assume you have a file called QuizGrades.csv with the following content:
Student1,100,90,80,90,80
Student2,70,80,85,85,80
Student3,100,100,100,95,100
Student4,70,70,80,70,80
Student5,90,90,90,90,100
Which of the following lines is the correct way to open a text file such as the above for reading.
File myFile = new File("QuizGrades.csv");
Scanner myScan = new Scanner(myFile);
Which of the following statements is true?
-A try/catch block should never be used with fileIO
-A try/catch block should be used only when writing to a file
-A try/catch block should be used only when reading from a file.
-A try/catch block should be used when reading and writing to a file.
A try/catch block should be used when reading and writing to a file.
Assume you have a file called QuizGrades.csv with the following content:
Student1,100,90,80,90,80
Student2,70,80,85,85,80
Student3,100,100,100,95,100
Student4,70,70,80,70,80
Student5,90,90,90,90,100
Which of the following code snips correctly reads each line from the file correctly assigning it to a variable called line.
You can assume the previous question correctly opened the file, and you have a variable myScan which allows you to access the file.
while(myScan.hasNextLine()) {
String line=myScan.nextLine();}