JAVA Midterm

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which of these keywords must be used to inherit a class?

extends

class Relational_operator { public static void main(String args[]) { int var1 = 5; int var2 = 6; System.out.print(var1 > var2); } }

false

Explain Polymorphism with an example code.

feature that allows us to perform a single action in different ways.

class Array_output { public static void main(String args[]) { char array_variable [] = new char[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = 'i'; System.out.print(array_variable[i] + " " ); i++; } } }

i i i i i

Which of the following tool used to execute java code.

java

Which of the tool is used to compile java code?

javacompile

What is process of defining two or more methods within same class that have same name but different parameters declaration? a) method overloading b) method overriding c) method hiding d) None of the mentioned

method overloading

public class Program4 { public static void main(String[] args) { int n = -23; while ((n % 2) == -1) { if (n == 2) break; n = n + 2; System.out.println("n = " + n); } } }

n = -21 n = -19 n = -17 n = -15 n = -13 n = -11 n = -9 n = -7 n = -5 n = -3 n = -1 n = 1

public class Test { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } public static void xMethod(int n) { n++; } }

n is 2

int[] list = { 5, 4, 3, 2, 1, 0 }; for (int i = 0; i < list.length-1; i++) { list[i] += list[i+1]; }

21

class Output { public static void main(String args[]) { int a = 1; int b = 2; int c; int d; c = ++b; d = a++; c++; b++; ++a; System.out.println(a + " " + b + " " + c); } }

3 4 4

class evaluate { public static void main(String args[]) { int a[] = {1,2,3,4,5}; int d[] = a; int sum = 0; for (int j = 0; j < 3; ++j) sum += (a[j] * d[j + 1]) + (a[j + 1] *d[j]); System.out.println(sum); } }

40

What is the output of the following fragment? for (int i = 4; i < 15; i++) { if (i % 4 == 1) System.out.print(i + " "); }

5 + 9 + 13

class Overload { int x; int y; void add(int a) { x = a + 1; } void add(int a, int b){ x = a + 2; } } class Overload_methods { public static void main(String args[]) { Overload obj = new Overload(); int a = 0; obj.add(6); System.out.println(obj.x); } }

7

class Array_output { public static void main(String args[]) { int array_variable[][] = {{1, 2, 3},{4 , 5, 6},{7, 8, 9}}; int sum = 0; for (int i = 0; i < 3; ++i) for (int j = 0; j < 3 ; ++j) sum = sum + array_variable[i][j]; System.out.print(sum / 5); } }

9

public class Main { public static void main(String[] args) { String str1 = "Hello"; String str2 = "hello"; if (str1 != str2) System.out.println("Hello"); else System.out.println("hello"); } }

Hello

Write a program that sums the integers in the range of 10 to 100 (inclusive) if the integer is evenly divisible by 4 or 6, but not both. For example, 16 should be part of the sum and 18 should be part of the sum, but not 24. Print the overall sum.

class Sum{ public static void main(String args[]){ int sum=0; for(int i=10;i<=100;i++){ if((i%4==0 || i%6==0)){ if(!((i%4==0) && (i%6==0))){ sum+=i; } } } System.out.println("The sum is "+sum); } }

class Main_class { public static void main(String args[]) { int x = 9; if (x == 9) { int x = 8; System.out.println(x); } } }

compilation error

Which of the following is a method having same name as that of its class? a) finalize b) delete c) class d) constructor

constructor

Suppose you enter input 2 3 6 from the console, when you run the program. What is the output? public class Test { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); double x = input.nextDouble(); double y = input.nextDouble(); double z = input.nextDouble(); System.out.println("(x < y && y < z) is " + (x < y && y < z)); System.out.println("(x < y || y < z) is " + (x < y || y < z)); System.out.println("!(x < y) is " + !(x < y)); System.out.println("(x + y < z) is " + (x + y < z)); System.out.println("(x + y < z) is " + (x + y < z)); } }

(x < y && y < z) is true (x < y || y < z) is true !(x < y) is false (x + y < z) is true (x + y < z) is true

class Array_output { public static void main(String args[]) { int array_variable [] = new int[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = i; System.out.print(array_variable[i] +" "); i++; } } }

0 1 2 3 4 5 6 7 8 9

class Box { int width; int height; int length; } class Mainclass { public static void main(String args[]) { Box obj1 = new Box(); Box obj2 = new Box(); obj1.height = 1; obj1.length = 2; obj1.width = 1; obj2 = obj1; System.out.println(obj2.height); } }

1

public class Test { public static void main(String[] args) { System.out.println(5 % 4); System.out.println(5 / 4); System.out.println(5 + 4 * 2); System.out.println((5 + 4) * 2); int x =1; x++; System.out.println(++x); x -= 4; System.out.println(x); } }

1 1 13 18 3 -1

class Jump_statments { public static void main(String args[]) { int x = 2; int y = 0; for ( ; y < 10; ++y) { if (y % x == 0) continue; else if (y == 8) break; else System.out.print(y + " "); } } }

1 3 5 7 9

Which of the following operators can operate on a boolean variable? 1. && 2. == 3. ?: 4. + = a) 3 & 2 b) 1 & 4 c) 1, 2 & 4 d) 1, 2 & 3

1, 2 & 3

class increment { public static void main(String args[]) { double var1 = 1 + 5; double var2 = var1 / 4; int var3 = 1 + 5; int var4 = var3 / 4; System.out.print(var2 + " " + var4); } }

1.5 1

class Operators { public static void main(String args[]) { int var1 = 5; int var2 = 6; int var3; var3 = ++ var2 * var1 / var2 + var2; System.out.print(var3); } }

12

public static void main(String[] args) { int a = 10; int b = 20; int[] c = { 5, 10, 15, 20 }; foo(a,b,c); // what are the values of a, b & c at this point in the program? } public static void foo(int x, int y, int[] z) { x += 5; y *= 2; for (int i = 0; i < z.length; i++) { z[i] -= 3; } }

15,40,{2,7,12,17}

What is the value stored in count after the following loop is executed? int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 2); System.out.println(count);

2

class A { int i; void display() { System.out.println(i); } } class B extends A { int j; void display() { System.out.println(j); } } class Inheritance_demo { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } }

2

class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class Inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } }

2 2

class Box { int width; int height; int length; } class Mainclass { public static void main(String args[]) { Box obj = new Box(); obj.width = 10; obj.height = 2; obj.length = 10; int y = obj.width x obj.height x obj.length; System.out.print(y); } }

200

Explain Abstract and Interface with an example code.

Abstract - A class which has the abstract keyword in its declaration is called abstract class. abstract class Shape { int b = 20; abstract public void calculateArea(); } public class Rectangle extends Shape { public static void main(String args[]) { Rectangle obj = new Rectangle(); obj.b = 200; obj.calculateArea(); } public void calculateArea() { System.out.println("Area is " + (obj.b * obj.b)); } } Interface - The interface is a blueprint that can be used to implement a class. interface Pet { public void test(); } class Dog implements Pet { public void test() { System.out.println("Interface Method Implemented"); } public static void main(String args[]) { Pet p = new Dog(); p.test(); } }

Which of the following is a valid declaration of an object of class Box? a) Box obj = new Box(); b) Box obj = new Box; c) obj = new Box(); d) new Box obj;

Box obj = new Box();

Which of these statement is incorrect? a) Every class must contain a main() method. b) Applets do not require a main() method at all. c) There can be only one main() method in a program. d) main() method must be made public.

Every class must contain a main() method.

Which of these is an incorrect Statement? a) It is necessary to use new operator to initialize an array. b) Array can be initialized using comma separated expressions surrounded by curly braces. c) Array can be initialized when they are declared. d) None of the mentioned

It is necessary to use new operator to initialize an array.

What is the stored in the object obj in following lines of code? Box obj; a) Memory address of allocated memory of object. b) NULL c) Any arbitrary pointer d) Garbage

NULL

Explain Overriding and Overloading and also write example codes.

Overriding - having two methods with the same method name and parameters class Dog{ public void bark(){ System.out.println("woof "); } } class Hound extends Dog{ public void sniff(){ System.out.println("sniff "); } public void bark(){ System.out.println("bowl"); }} Overloading - when two or more methods in one class have the same method name but different parameters class Dog{ public void bark(){ System.out.println("woof "); } //overloading method public void bark(int num){ for(int i=0; i<num; i++) System.out.println("woof "); } }

Which of the following statements is correct? a) Public method is accessible to all other classes in the hierarchy b) Public method is accessible only to subclasses of its parent class c) Public method can only be called by object of its class. d) Public method can be accessed by calling object of the public class.

Public method is accessible to all other classes in the hierarchy

Which of these is returned by greater than, <, and equal to, ==, operator? a) Integers b) Floating - point numbers c) Boolean d) None of the mentioned

boolean

Which one is a valid declaration of a boolean? a) boolean b1 = 1; b) boolean b2 = 'false'; c) boolean b3 = false; d) boolean b4 = 'true'

boolean b3 = false;

Write a program that prompts the user 5 times for integer values, printing the current average after each number is entered. Print the average to two decimal places.

import java.util.*; import java.lang.*; import java.io.*; class Average{ public static void main (String[] args) throws java.lang.Exception{ int i;double avg = 0; Scanner reader = new Scanner(System.in); for(i =1;i<=5;i++){ System.out.println("Enter a number: "); int n = reader.nextInt(); avg = avg+(n-avg)/i; System.out.println("The average of the first "+ i+ " numbers is "+String.format( "%.2f", avg )); } } }

Write a program that prompts the user for a positive integer n and then produces an n × n multiplication table using a nested for loop. You do not have to do any error-checking nor do you need to put row or column labels.

import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter a positive integer : "); int number=sc.nextInt(); for(int i=1;i<=number;i++) { for(int j=1;j<=number;j++) { System.out.print(i*j+" "); } System.out.println(); } } }

Write a program that prompts the user for positive integers, only stopping when a negative integer or zero is given. The program should then print out how many of the positive integers were odd.

import java.util.Scanner; public class Main2{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n, count = 0; while (true) { System.out.print("Enter a positive integer (0 or negative to stop): "); n = scanner.nextInt(); if(n<=0){ break; } if(n%2 == 1){ count += 1; } } System.out.println("You entered "+count+" odd integers."); } }

Make a program that displays n prime numbers. n is a user input.

import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); System.out.print("Enter a positive integer: "); int n = scanner.nextInt(); if(n>=1){ System.out.print(2); boolean flag = false; for(int i = 3;n>=2;i++){ flag = false; for(int j = 2;j<i-1;j++){ if(i%j == 0){ flag = true; } } if(!flag){ System.out.print(", "+i); n--; } } } } }

Write a function that when given two integers, x and y, returns the maximum divisor of x or y, excluding x or y themselves. For example, if the two integers are 8 and 9, the divisors of 8 (excluding 8) are 1, 2, and 4 while the divisors of 9 (excluding 9) are 1 and 3. The maximum is 4.

import java.util.Scanner; public class MaxDivisor { public static int maximumFactor(int x, int y) { for (int i = Math.min(x-1, y-1);i >= 1; i--) { if (x % i == 0 || y % i == 0) { return i; } } return 0; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter two integers: "); int x = in.nextInt(), y = in.nextInt(); System.out.println("Maximum divisor of " + x + " and " + y + " is " + maximumFactor(x, y)); in.close(); } }

Write a program that prompts the user for a positive integer, n, and then prints a following shape of stars using nested for loop, System.out.print("*");, and System.out.println();. Example1:

import java.util.Scanner; public class NestedLoopsPattern { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a positive integer: "); int n = in.nextInt(); for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); } System.out.println(); } for (int i = n-1; i >= 1; i--) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } in.close(); } }

Finish the program below by adding the code to print the running average, where the average is based on a moving set of k values. For example, when k = 3, the program would print the average of {1, 2, 3}, then the average of {2, 3, 10}, then the average of {3, 10, 9}, and so forth, finishing with the average of {9, 10, 13}. Print the average to three decimal places. The code that you write should not be hard-coded to this particular data except for the fact that there are nine values in the list. Int k is given by user input. int[] d = {1, 2, 3, 10, 9, 8, 4, 5, 6, 6, 5, 6, 9, 10, 13}; int k;

import java.util.Scanner; public class RunningAverage { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] d = {1, 2, 3, 10, 9, 8, 4, 5, 6, 6, 5, 6, 9, 10, 13}; int k; System.out.print("Enter a value for k: "); k = in.nextInt(); double sum = 0; System.out.println("Running averages are"); for (int i = 0; i < d.length; i++) { if (i >= k) { System.out.printf("%.3f\n", sum/(double)k); sum -= d[i-k]; } sum += d[i]; } System.out.printf("%.3f\n", sum/(double)k); } }

Write a program that prompts the user for a positive integer n and then sums the integers in the range of 100 to 200 (inclusive) that are evenly divisible by n. The program will print the final sum. You do not have to do any error-checking.

import java.util.Scanner; public class SumRange { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a positive integer: "); int n=sc.nextInt(); int sum=0; for(int i=100;i<=200;i++) { if(i%n==0) sum+=i; } System.out.println("The sum is "+sum); } }

Which of these is an incorrect array declaration? a) int arr[] = new int[5]; b) int [] arr = new int[5]; c) int arr[]; arr = new int[5]; d) int arr[] = int [5] new;

int arr[] = int [5] new;

public class Midterm { public static void main(String[] args) { int lhs = 9; int rhs = 3; int[] list = { 5, 4, 3, 2, 1 }; scram(list, lhs, rhs); System.out.println("Main: lhs = " + lhs + ", rhs = " + rhs); System.out.println("Main: list[3] = " + list[3]); } public static void scram(int[] list, int lhs, int rhs) { list[rhs] = lhs; lhs = list[1]; rhs = list[list.length - 1]; System.out.println("scram: lhs = " + lhs + ", rhs = " + rhs); System.out.println("scram: list[3] = " + list[3]); } }

scram: lhs = 4, rhs = 1 scram: list[3] = 9 Main: lhs = 9, rhs = 3 Main: list[3] = 9

Which of these statements is correct? a) true and false are numeric values 1 and 0. b) true and false are numeric values 0 and 1. c) true is any non zero value and false is 0. d) true and false are non numeric values.

true and false are numeric values 1 and 0.

public class Test { public static void main(String[] args) { int x = 9; int y = 8; int z = 7; if (x > 9) if (y > 8) System.out.println("x > 9 and y > 8"); else if (z >= 7) System.out.println("x <= 9 and z >= 7"); else System.out.println("x <= 9 and z < 7"); } }

x <= 9 and z >= 7

public class Program1 { public static void main(String[] args) { int x = 4; while ( x <= 8 ) { for ( int y = x; y > 1; y = y - 1 ) { if ( y % 2 == 0 ) System.out.print( "x" ); else System.out.print( "y" ); } System.out.println(); x = x + 1; } } }

xyx yxyx xyxyx yxyxyx xyxyxyx


Conjuntos de estudio relacionados

1 Introduction to Data Structures and Algorithms

View Set

An Occurrence at Owl Creek Bridge

View Set

10th Grade L.A. Literary Terms FINAL

View Set

Unit 5: Lesson 1: Early Astronomy Assessment Questions

View Set

Geometry Lesson 1.1 Vocabulary, Geometry Lesson 1.2 Vocabulary, Geometry Lesson 1.3 Vocabulary, Geometry Lesson 1.4 Vocabulary, Geometry Lesson 1.5 Vocabulary

View Set

human growth and development unit 4 test: socioemotional development in middle and late childhood review ANSWERS

View Set