CIS 2830-02 Final Exam - Java

Ace your homework & exams now with Quizwiz!

A literal could be...

A double, string, integer, but not a variable, class, or method

Why should we use methods?

Creating a new method gives you an opportunity to give a name to a group of statements, which makes code easier to read and understand. A common problem solving technique is to break tasks down into sub-problems. Introducing new methods can make a program smaller by eliminating repetitive code

What is the output of the following program? public class Ex4_2{ public static void zoop() { baffle(); System.out.print("you foo "); baffle(); } public static void main(String[] args) { System.out.print("No, I "); zoop(); System.out.print("I "); baffle(); } public static void baffle() { System.out.print("baz"); ping(); } public static void ping() { System.out.println("."); baffle(); } }

The program goes forever.

The following statement is... String message;

a declaration, it defines message as a variable or type of String

What is a Javadoc?

a document generator tool in Java programming language for generating standard documentation in HTML format

the following piece of code found in a program that compiles is an example of: System.out.println("this program prints the first 3 even integers after 0:"); System.out.println(2); System.out.println(4); System.out.println(7);

a logic error

TRUE or FALSE in all the examples of programs taking input in class we have declared a variable of type Scanner and have created an instance of Scanner.

TRUE

TRUE or FALSE in order to use class Scanner you must import it from the package java.util

TRUE

TRUE or FALSE the class Scanner has several methods that allow a java program to get input.

TRUE

What does the following fragment print? String foo = "hello"; System.out.print("The book says: "); System.out.println(foo);

The book says: hello

What does the following fragment print: // generate some simple output System.out.println("Hello, World!")

The fragment is illegal due to the missing ; at the end of the line

The following code: public class Echo { public static void main(String[] args) { String line; Scanner input = new Scanner(System.in); } }

it does not compile

The following code: public class foo { public static void main(String [] aaa){ final double CM_PER_INCH = 2.54; CM_PER_INCH = 2.54; System.out.println(CM_PER_INCH); } }

it does not compile

what would the following piece of code print System.out.println("Hello" + 6-4);

it has a compile-time error

What does the following fragment print: System.out.println(1.0/0);

it would keep going to infinity

Which of the following commands will run the compiled Java program named DoItNow?

java DoItNow

Which of the following commands will compile the Java program in file DoItNow.java?

javac DoItNow.java

what would this fragment of code print: String x = "my"; x = "name"; x= "is"; x = "jon";

jon

Consider following code. In what order are the methods executed? public class TJ04_24 { public static void A(){ D(); G(); } public static void B(){ } public static void C(){ B(); F(); } public static void D(){ } public static void E(){ A(); C(); } public static void F(){ D(); } public static void G(){ B(); } public static void main(String[] args) { E(); } }

mainEADGBCBFD

TRUE or FALSE One of the methods of the class Math is called atan.

TRUE

TRUE or FALSE The code resulting from the translation by the compiler is called object code

TRUE

TRUE or FALSE Using printf incorrectly may cause a runtime error, i.e. your program will compile but it will crash when you try to run it

TRUE

TRUE or FALSE a class can contain several methods

TRUE

what would this print: System.out.printf("'%08.2f'\n", 10.3456);

'00010.35'

What is printed by the following fragment? int x = 10; System.out.println(x/20.0);

0.5

what is printed by the following code fragment System.out.print(1000%10); System.out.print(100%10); System.out.print(10%10); System.out.println(1%10);

0001

what would the following g code fragment print x = 21.12345; System.out.printf("%08.3f %08.3f %08.3f \n",x,x*10,x*100);

0021.123 0211.234 2112.345

if the value of x is 5, what does the following fragment of code print? System.out.print(x%5); System.out.print((x-1)%5); System.out.print((x-2)%5); System.out.print((x-3)%5); System.out.print((x-4)%5); System.out.println((x-5)%5);

043210

What does the following fragment print? int x = 10; System.out.println(1 + x * 5);

51

the following fragment of code print: y = 1.2345; x = (int) y; System.out.println(x);

1, the value of y is converted into an integer x

Suppose we have a method mm(int x). that contains only the following statement: x = 5; what is printed if the main method has the following statements: int x = 10; mm(x); System.out.println(x);

10

What is printed by the following fragment? double x=10/20; System.out.println(x);

10/20

How much is 2 multiplies by itself 20 times?

1048576.0

what would the following piece of code print System.out.println(6+4 + "Hello");

10Hello

What does the following fragment print? System.out.println(230 - 220 * 0.5);

120.0

the following piece of code found in a program that compiles would print: x = 5;y = 10;x = x + y; System.out.println(x);

15

What does the following fragment print: int x = 1; System.out.println(1 + "x" + 1 );

1x1

what does this print final double cm_per_inch = 2.54; System.out.println(cm_per_inch);

2.54

what does this print final double cm_per_inch; cm_per_inch = 2.54; System.out.println(cm_per_inch);

2.54

the print statements of this program have been numbered (\\1,\\2,\\3,\\4). In what order are they executed class foo{ public static void zonk1(String st){ System.out.print(st);//1 } public static void zonk2(String st){ System.out.print(st);//2 } public static void zonk3(String st){ System.out.print(st);//3 } public static void main(String[] args){ zonk2("foo"); zonk3("foo"); zonk1("foo"); System.out.println();//4 } }

2314

what would this fragment of code found in a program that complies successfully print: x = 3*3-1*4-4/2; System.out.println(x);

3

in the following code the print statements are numbered (the numbers look like //1, //2, //3, //4) list the numbers in the order in which they are executed. public class foo{public static void main(String[] args) { zippo("foo"); System.out.println("foo");//1 baffle("zoo"); } public static void baffle(String blimp) { System.out.println(blimp);//2 } public static void zippo(String quince) { System.out.println(quince);//3 System.out.println(quince);//4 } }

3412

In the following code, how many literals are there? public class Convert{ public static void main(String [] args){ int inch; double cm; Scanner in = new Scanner(System.in); System.out.print("How many inches? "); inch = in.nextInt(); cm = inch * 2.54; System.out.print(inch + " in = "); System.out.println(cm + " cm"); } }

4

After the following code is executed, what is the value of B? int a = 5; int b = a; // a and b are now equal a = 3;

B = 5

What does the following fragment print: System.out.println(.1 + .1 + .1);

It wouldn't necessarily print 0.3

TRUE or FALSE A high level programming language is a language that can be executed at a higher speed

FALSE

TRUE or FALSE An algorithm is a computer program

FALSE

TRUE or FALSE An expression can contain several statements

FALSE

TRUE or FALSE Byte code is machine code that is executed directly by the cpu of most machines

FALSE

TRUE or FALSE Each class can have only one method

FALSE

TRUE or FALSE In order to run in Windows, a Java program compiled in Unix has to be compiled in Windows first.

FALSE

TRUE or FALSE Java is not an interpreted language

FALSE

TRUE or FALSE The Java Virtual Machine (JVM) is a piece of hardware

FALSE

TRUE or FALSE The java virtual machine is a chip manufactured by Intel and it is found in most modern computers.

FALSE

TRUE or FALSE The longest possible long is 922,337,203,685

FALSE

TRUE or FALSE Variable names are not case sensitive.

FALSE

TRUE or FALSE given the next two lines of code: Scanner foo; the first line declares a variables called foo of type Scanner. after this declaration you can use: foo.nextInt() because nextInt() is a method of Scanner

FALSE

TRUE or False A compiler reads the whole program and executes it

FALSE

TRUE or False Java is not a compiled language

FALSE

TRUE or False in order to use the Math class you must first import the package that contains it.

FALSE

TRUE or FALSE These are all valid java identifiers: $_____$ FALSE NEW new_2 tax-rate-2021

FALSE, - cannot be used as an identifier but FALSE and NEW can be used as identifiers despite being reserve words due to Java being cap-sensitive. So NEW is not the same as new, a reserve word.

TRUE or FALSE You can use any name you want for a variable.

FALSE, java has reserved words that cannot be used as a variable

TRUE or FALSE Either of the two following words can be used as variable names. new & old

FALSE, new is a reserved word in java

TRUE or FALSE A variable of type Int can be used to store integers

FALSE, there is no type INT but rather a form of integer

If the user enters 21 in response to the question "What is your age?", and Jon in response to the question "What is your name?", the following code prints import java.util.Scanner; public class foo { public static void main(String [] aaa){ Scanner in = new Scanner(System.in); System.out.print("What is your age? "); int age = in.nextInt(); System.out.print("What is your name? "); String name = in.nextLine(); System.out.printf("Hello %s, age %d\n", name, age); } }

Hello , age 21

what would the following piece of code print System.out.println("Hello" + 6-4);

Hello2

what would the following piece of code print System.out.println("Hello" + 6+4);

Hello64

What is the output of the following program? Be precise about where there are spaces and where there are newlines. public class Ex4_1{ public static void zoop() { baffle(); System.out.print("you foo "); baffle(); } public static void main(String[] args) { System.out.print("No, I "); zoop(); System.out.print("I "); baffle(); } public static void baffle() { System.out.print("baz"); ping(); } public static void ping() { System.out.println("."); } }

No, I baz. you foo baz, I baz

TRUE or FALSE Before a program written in a high level language can be run it has to be translated completely into a low-level language

TRUE

TRUE or FALSE Code written in a high-level language is called source code.

TRUE

TRUE or FALSE In Java each variable has a type that determines what kind of values it can store.

TRUE

TRUE or FALSE In java, capitalization matters. (EX: System.out.println and system.out.println)

TRUE

TRUE or FALSE A class may contain several methods.

TRUE

TRUE or FALSE A comment in a Java program is a portion of the code that is ignore by the compiler or interpreter.

TRUE

TRUE or FALSE A compiler reads the whole program and translates it into executable code

TRUE

TRUE or FALSE According to the book input and output are two of the types of instructions that make up every computer program.

TRUE

TRUE or FALSE An interpreter executes the instructions of a high-level language one by one.

TRUE

TRUE or FALSE Basic mathematical operations is one of the elements used to create computer programs.

TRUE

What does the following mean: 7 = a

This assignment order is illegal because the variable should come first before the assigned value or string.

what would this fragment of code print: x = "hello"; System.out.println(x*3);

This code would not compile as the variable x is not declared as a variable type.

What is printed by the following fragment: int x = 10.0; System.out.println(x/20);

This fragment is illegal because a double is assigned to an integer.

does the following program compile and run import java.util.*; public class foo {public static void main(String[] args) { String line; Scanner input = new Scanner(System.in); int x = input.nextInt(); } }

Yes

What does the following mean: message = "Hello!"

an assignment statement, "Hello!" is assigned to the String variable message

what would the following fragment of code found in a program that compiles successfully print: a = 0;x = 3/a; System.out.println(x);

an error occurs when the program runs (run-time error)

Suppose a class has the following method: public static void printTime(int x) { hour = 3*hour; System.out.print(hour); } if the main method of the class contains the following code: public static void main(String[] args) { int hour = 1; printTime(hour); System.out.println(hour); } what would the main method print when it is executed.

compile-time error

Suppose we have a method mm(int y). that contains only the following statement: System.out.print(x); what is printed if the main method only has the following statements: int x = 10; mm(x); System.out.println(x);

compile-time error

the following piece of code is an example of: int x = "hello " + 5; System.out.println(x);

compile-time error

what does this print final double cm_per_inch = 2.54; cm_per_inch = 2.54; System.out.println(cm_per_inch);

compile-time error

what would the following fragment print y = 1; x = (String) y; System.out.println(x);

compile-time error

What does the following fragment print? String foo = "hello"; System.out.println("foo");

foo

What does the following mean: int hour = 11;

declares and assigns a value to the variable house on the same line

Suppose you have a static method called printAmerican that takes the month (e.g. "July"), day (e.g. 23), year (e.g. 2018), and day of the week (e.g. "Thursday") as parameters and displays them in American format (e.g. Thursday, July 23, 2018). Which one of the following programs calls the method correctly to print the date.

public class PrintAmerican{ //method diefition goes here public static void main(String[] aaa){ PrintAmerican("July", 23, 2018, "Thursday"); } }

Choose what would be the correct first line of a method named zool that takes three parameters: an int and two Strings.

public void zool(int i, String s1, String s2)

The variables in a program and their current values are called:

the program's state

what is printed by this program class foo{ public static void zonk(String st1, String st2, String st3){ System.out.print(st3 + " " + st1 + " " + st2 + "\n"); } public static void main(String[] args){ zonk("this","method has","three arguments"); } }

three arguments this method has

In a java program a literal is a:

value

if System.out.println(x%21); prints 0, it means

x is a multiple of 21

To declare an integer variable named x, you simply type, which means... int x;

x is a variable that can be used to store values of type int

What does the following fragment print: int x = 1; System.out.println("x" + 1 + 1 );

x11

Identify the line of code that calls a method named zool, that takes three parameters: an int and two Strings, passing as arguments the value 11, the String "hello" and the String "world".

zool(11,"hello", "world")


Related study sets

Disability Income and Related Insurance Quiz

View Set

Ch10 Cost of Prod. & P.C. By Kevin Crump

View Set