Java Eclipse Flashcards

1
Q

What’s the first thing u do after creating a java project in eclipse

A

You create a package in src

Classes are created inside that package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to get used input in java

A
Scanner scan = new Scanner(System.in);
X = scan.nextInt();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to create a sub class in java

A

public class child extends parent {…}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does JUnit work?

A

JUnit create a class separate from the class under test (business class). Inside this test class, we instantiate a business class object and specific methods in business class instance are called with known arguments and returned value is compared with known correct value using assrtEquals.

Each test method is annotated with
@Test

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In Eclipse, how do u create JUnit test case?

A

Right click on the class to be tested -> JUnit Test Case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some assertion methods used in JUnit?

A
  • assertEquals()
  • assertNotEquals()
  • assertTrue()
  • assertNull()
    etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are some annotations used in JUnit?

A

@BeforeClass: This method is executed before any test is run. Used for setting up. Business class object may be instantiated here and used in other test cases.

@Before: This method is executed before each test case is run.

@Test: This annotation is prefixed before each test method.

@After: This method is executed after each test case is run.

@AfterClass: This method is executed after ALL test cases are run. Typically used to teardown the set up and free all resources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When is a jar file runnable?

A

The jar file should contain a class with a main() method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to work with jar file?

A
Create jar file with a bunch of compiled java class files, with following cmd,
jar cvf  newFileName.jar  first.class second.class …

Check contents of jar file
jar tvf filename.jar

To run a jar file, we have to know the class we want to run,  say  xyz.class
Find full path to the class inside jar file

jar tvf filename.jar
Locat the line containing xyz.class, say
aa/bb/cc/xyz.class

run with following cmd,

java -cp /path/to/jar/file.jar aa/bb/cc/xyz

or

java -cp /path/to/jar/file.jar aa.bb.cc.xyz

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a package in java?

A

All classes should specifically be placed in named packages.

When you define a class, make first line in the file as

package myOwnPackage;

Then define your class, this will put the class in “myOwnPackage” pkg.

In other files, where that class is needed, put

import myOwnPackage;

import java.util.Date; // wil import Date class

import java.util.*; // Will import all classes in java.util

==> Date x = new Date();

If u don’t want to import entire package, use following,
java.util.Date x = new java.util.Date(); // No need for import

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is java classpath?

A

When u run a jar file, u have to explicitly specify classpath using -cp, eg.

java -cp /path/to/jar/file.jar aa.bb.cc.xyz

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the structure of main() function in java?

A

public static void main(String[] args){
System.out.println(“Test - 1”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is single import declaration?

A

When you import only one class from a package.

U explicitly specify packageName and className, like,

import pkgName.className;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do u import all classes from a package?

A

import pkgName.;
import java.util.
;

This will import all classes from that pkg, BUT NOT FROM sub-packages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When compiling a class file which specifies a package, how do u create a directory structure at the time of compilation?

A

With -d option,

javac -d . myClass.java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a servelet?

A

It is a part of web server which dynamically creates web pages and sends to browser. This is as opposed to sending/serving static HTML files.