private void handleCliConfig(AbstractCliConfig config) throws Exception {
    Set<File> qfiles = config.getQueryFiles();
    for (File file : qfiles) {
      String baseName = file.getName();
      String rd = config.getResultsDir();
      File of = new File(rd, baseName + ".out");
      if (outsNeeded.containsKey(of)) {
        System.err.printf("duplicate: [%s;%s] %s\n", config.getClass().getSimpleName(),
            outsNeeded.get(of).getClass().getSimpleName(), of);
        // throw new RuntimeException("duplicate?!");
      }
      outsNeeded.put(of, config);
    }

    File od = new File(config.getResultsDir());
    for (File file : od.listFiles(new QOutFilter())) {
      outsFound.add(file);
    }
  }
This test ensures that there are no dangling q.out files in the project

It has a cli functionlity to remove them if there are any.
itests/util/src/test/java/org/apache/hadoop/hive/cli/control/TestDanglingQOuts.java